Post a file through https using indy / delphi components

自作多情 提交于 2019-12-04 16:04:49

HTTP.Request.Host := RemoteHost;

Don't set that manually. TIdHTTP will manage that for you.

HTTP.Request.Accept := 'multipart/mixed';

You are telling the server that you will only accept responses that are multipart/mixed, is that what you really want?

HTTP.Request.ContentType := TestStream.RequestContentType;

Don't set that manually when sending a TIdMultipartFormDataStream, Post() will handle that for you.

HTTP.Post('https://www.remotehost.com/controller?function=submitfile',TestStream, Resultado); memResultado.Lines.Add(Resultado.DataString);

Is the server sending the response in the same charset that you initialized Resultado with? If not, then reading the DataString property may fail and return a blank string (TEncoding does not raise an exception when it fails to encode/decode a string). You should let Indy decode the response data for you, since it knows the response's type and charset:

var
  Resultado: string;

Resultado := HTTP.Post('https://www.remotehost.com/controller?function=submitfile', TestStream);
memResultado.Lines.Add(Resultado);

The server does not send any error. Just the file is not uploaded.

If the server is not sending back an HTTP error response (which would cause TIdHTTP to raise an exception), then it is either:

  1. sending back an HTTP success response, but sending an error message in the response data.

  2. accepting the file, but then discarding it

  3. accepting the file, but saving it under a different path/name than you are expecting.

Hard to say for sure since you did not show the actual HTTP request/response data that is being transmitted.

Update: The server IS sending back an error, using #1 above. The server is sending back an HTTP 200 OK reply, but the content of the reply says 'Failure!' (in fact, your original code should have been logging that message in your TMemo). That is why TIdHTTP is not raising an exception. It only looks for HTTP errors, not error messages in body content. You will have to add extra code to account for that possibility, eg:

Resultado := HTTP.Post('https://www.remotehost.com/controller?function=submitfile', TestStream);
if TextStartsWith(Resultado, 'Failure') then
begin
  // Upload failed, do something...
end;

As for why the server is failing in the first place, you will have to contact the server admin and ask about that. The admin will likely have server-side logs to troubleshoot with.

However, I will mention that TIdMultipartFormDataStream does currently send a Content-Type: text/plain header for text fields, which is fine (even encouraged) for HTML 4 forms, but is not OK (it is forbidden) for HTML 5 forms, and some servers do fail if a Content-Type header is present for a text field. HTML5 only allows it on file fields. This is a known issue with TIdMultipartFormDataStream that is already being worked on, but there is no ETA on when the fix will be available. But you can ask the server admin how the server reacts to that condition.

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