How do I upload a file using http post? Delphi 2009

人走茶凉 提交于 2020-07-05 07:26:56

问题


My goal is to upload a text file via HTTP post I am using Delphi 2009.

Say for example to the following URL

https://www.example.com/ex/exampleAPI.asmx/Process

I understand it can be done by using the TIdHttp component. And the following call

IdHttp1.Post();

But I cant figure out how to set up everything i.e. specifying the url and including the file to be posted.

Thanks.


回答1:


TIdHTTP has two overloaded versions of Post() that take a filename as input:

var
  Response: String;

Response := IdHTTP1.Post('https://www.example.com/ex/exampleAPI.asmx/Process', 'c:\filename.txt');

.

var
  Response: TStream;

Response := TMemoryStream.Create;
IdHTTP1.Post('https://www.example.com/ex/exampleAPI.asmx/Process', 'c:\filename.txt', Response);
...
Response.Free;

Note that you are posting to an HTTPS URL, so you need to first assign an SSL-enabled IOHandler, such as TIdSSLIOHandlerSocketOpenSSL, to the TIdHTTP.IOHandler property beforehand.



来源:https://stackoverflow.com/questions/10889511/how-do-i-upload-a-file-using-http-post-delphi-2009

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!