[英]Error 500 Failed to upload file using Delphi Indy
I'm trying to upload a file to oboom.com i logged in successfully but when try to post the file i get that error 我正在尝试将文件上传到oboom.com我成功登录但是当尝试发布文件时我得到了该错误
HTTP/1.1500 Internal Server Error.
HTTP / 1.1500内部服务器错误。
with this respose text 用这个respose文本
[500,"illegal post header","Content-Transfer-Encoding"]
procedure TForm1.Button1Click(Sender: TObject);
var
S: tstringlist;
html: string;
clHttpRequest1: tclHttpRequest;
SSLIOHandlerSocketOpenSSL: TIdSSLIOHandlerSocketOpenSSL;
params: TIdMultiPartFormDataStream;
HTTP, HTTP2: tidhttp;
begin
params := TIdMultiPartFormDataStream.Create;
S := tstringlist.Create;
HTTP2 := tidhttp.Create(nil);
try
cookie := tidcookiemanager.Create(nil);
SSLIOHandlerSocketOpenSSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
HTTP2.IOHandler := SSLIOHandlerSocketOpenSSL;
HTTP2.HandleRedirects := False;
HTTP2.ConnectTimeout := 10000;
HTTP2.ReadTimeout := 10000;
HTTP2.CookieManager := cookie;
HTTP2.AllowCookies := True;
HTTP2.Request.UserAgent :=
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36';
HTTP2.Request.ContentType := 'application/x-www-form-urlencoded';
HTTP2.Request.ContentEncoding := 'gzip, deflate';
HTTP2.Request.Accept := '*/*';
HTTP2.Request.Connection := 'Keep-Alive';
S.Values['auth'] := 'email@gmail.com';
S.Values['pass'] := 'password';
S.Values['app_id'] := '5hwaJUcDicXprlV3gjaB';
S.Values['app_session'] := '288196272';
html := HTTP2.Post('https://www.oboom.com/1.0/login', S);
finally
HTTP2.Free;
S.Free;
end;
token := ExtractBetween(html, 'session":"', '"');
try
HTTP := tidhttp.Create;
HTTP.HandleRedirects := True;
HTTP.Request.Connection := 'keep-alive';
HTTP.AllowCookies := True;
HTTP.CookieManager := cookie;
HTTP.Request.Referer := 'http://upload.oboom.com';
HTTP.Request.ContentType :=
'multipart/form-data; boundary=----------GI3Ef1cH2GI3gL6ae0Ef1KM7Ef1gL6';
HTTP.Request.Accept := '*/*';
HTTP.Request.AcceptEncoding := 'gzip,deflate';
HTTP.ConnectTimeout := 20000;
HTTP.ReadTimeout := 20000;
HTTP.Request.UserAgent :=
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36';
params.AddFile('file', 'C:\Users\M\Pictures\Martin.jpg', );
HTTP.Post('http://upload.oboom.com/1.0/ul?token=' + token +
'&parent=1&name_policy=rename', params);
finally
HTTP.Free;
params.Free;
cookie.Free;
end;
memo1.Text := html;
end;
i have googled hours for a solution but no luck :/ 我用Google搜索了几个小时的解决方案,但没有运气:/
tried this way : 试过这种方式:
Params.AddFile('file', 'C:\Users\M\Pictures\Martin.jpg','application/octet-stream');
Params.AddFile('file', 'C:\Users\M\Pictures\Martin.jpg','multipart/form-data');
but same error 但同样的错误
i have tried clever internet compenent and succeeded uploading the file but i would like to use indy .. 我已经尝试过聪明的互联网功能并成功上传文件,但我想使用indy ..
i use delphi X3 我使用delphi X3
You need to get rid of these lines: 你需要摆脱这些线:
HTTP2.Request.ContentEncoding := 'gzip, deflate';
HTTP.Request.AcceptEncoding := 'gzip,deflate';
TIdHTTP
manages those values for you based on whether its Compressor
property is assigned and enabled. TIdHTTP
根据是否已分配和启用Compressor
属性来为您管理这些值。 And, you are not sending a compressed request, so those values should not be used anyway. 并且,您没有发送压缩请求,因此无论如何都不应使用这些值。
Also, you need to get rid of this line: 此外,你需要摆脱这一行:
HTTP.Request.ContentType :=
'multipart/form-data; boundary=----------GI3Ef1cH2GI3gL6ae0Ef1KM7Ef1gL6';
Post()
manages that value for you, especially the boundary
, which TIdMultipartFormDtaStream
generates dynamically. Post()
为您管理该值,尤其是TIdMultipartFormDtaStream
动态生成的boundary
。
Update the only other place that Content-Transfer-Encoding
is used is on the individual fields of the TIdMultipartFormDataStream
. 更新使用
Content-Transfer-Encoding
的唯一其他位置是TIdMultipartFormDataStream
的各个字段。 Each TIdFormDataField
has a ContentTransfer
property. 每个
TIdFormDataField
都有一个ContentTransfer
属性。 AddFile()
initializes it to 'binary'
, but you can also set it to a blank string to disable the header: AddFile()
将其初始化为'binary'
,但您也可以将其设置为空字符串以禁用标题:
params.AddFile(...).ContentTransfer := '';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.