C++Builder XE2怎么通过IDHTTP的POST方法上传文件
1个回答
推荐于2016-01-20 · 知道合伙人影视综艺行家
关注
展开全部
C++Builder XE2如何通过IDHTTP的POST方法上传文件?
DEPHI的代码如下:
var
MultiPartFormDataStream: TMsMultiPartFormDataStream;
idhttp1:TIdHTTP;
begin
MultiPartFormDataStream := TMsMultiPartFormDataStream.Create;
try
id(nil);
IdHTTP1.ProtocolVersion:=pv1_1;
IdHTTP1.HandleRedirects := True;
IdHTTP1.Request.UserAgent:='Klive';
IdHTTP1.Request.Connection:='Keep-Alive';
IdHTTP1.HTTPOptions:=IdHTTP1.HTTPOptions+[hoKeepOrigProtocol];
IdHttp1.Request.ContentType := MultiPartFormDataStream.RequestContentType;
MultiPartFormDataStream.AddFormField('name', 'file');
MultiPartFormDataStream.AddFormField('filename', Path);
MultiPartFormDataStream.AddFile('file', filename, 'testmime');
MultiPartFormDataStream.PrepareStreamForDispatch;
MultiPartFormDataStream.Position := 0;
try
Result:=pchar(id(str,MultiPartFormDataStream));
except
on e: EIdHTTPProtocolException do
begin
result:= pchar(e.ErrorMessage);// Application.MessageBox(PChar(e.ErrorMessage), 'ERROR', MB_OK + MB_ICONSTOP);
end;
end;
finally
MultiPartFormDataStream.Free;
这个代码是金山快盘上传文件的实现代码,具体要求是这样的:
POST /cdlnode/1/fileops/upload_file oauth_nonce=57895862&oauth_timestamp=1328956550&oauth_consumer_key=xcKXXgo9AkXdJuBn&oauth_signature_method=HMAC-SHA1&oauth_version=1.0&oauth_token=fe6265afa5c74866a62573b34ca411fb&oauth_signature=eQ9BhjCaIpUAWDD3cbb%2BrtlfXt4%3D&path=%2Fttt1328956550.95sss%2Ftestw.wps&root=app_folder&overwrite=True HTTP/1.1
Accept-Encoding: identity
Content-Length: 237
Host: ufaclien
Content-Type: multipart/form-data; boundary=----------ThIs_Is_tHe_bouNdaRY_$
Connection: close
User-Agent: klive
------------ThIs_Is_tHe_bouNdaRY_$
Content-Disposition: form-data; name="file"; filename="testw.wps"
Content-Type: application/octet-stream
1328956550.991328956550.991328956550.991328956550.99
------------ThIs_Is_tHe_bouNdaRY_$--
DEPHI的代码如下:
var
MultiPartFormDataStream: TMsMultiPartFormDataStream;
idhttp1:TIdHTTP;
begin
MultiPartFormDataStream := TMsMultiPartFormDataStream.Create;
try
id(nil);
IdHTTP1.ProtocolVersion:=pv1_1;
IdHTTP1.HandleRedirects := True;
IdHTTP1.Request.UserAgent:='Klive';
IdHTTP1.Request.Connection:='Keep-Alive';
IdHTTP1.HTTPOptions:=IdHTTP1.HTTPOptions+[hoKeepOrigProtocol];
IdHttp1.Request.ContentType := MultiPartFormDataStream.RequestContentType;
MultiPartFormDataStream.AddFormField('name', 'file');
MultiPartFormDataStream.AddFormField('filename', Path);
MultiPartFormDataStream.AddFile('file', filename, 'testmime');
MultiPartFormDataStream.PrepareStreamForDispatch;
MultiPartFormDataStream.Position := 0;
try
Result:=pchar(id(str,MultiPartFormDataStream));
except
on e: EIdHTTPProtocolException do
begin
result:= pchar(e.ErrorMessage);// Application.MessageBox(PChar(e.ErrorMessage), 'ERROR', MB_OK + MB_ICONSTOP);
end;
end;
finally
MultiPartFormDataStream.Free;
这个代码是金山快盘上传文件的实现代码,具体要求是这样的:
POST /cdlnode/1/fileops/upload_file oauth_nonce=57895862&oauth_timestamp=1328956550&oauth_consumer_key=xcKXXgo9AkXdJuBn&oauth_signature_method=HMAC-SHA1&oauth_version=1.0&oauth_token=fe6265afa5c74866a62573b34ca411fb&oauth_signature=eQ9BhjCaIpUAWDD3cbb%2BrtlfXt4%3D&path=%2Fttt1328956550.95sss%2Ftestw.wps&root=app_folder&overwrite=True HTTP/1.1
Accept-Encoding: identity
Content-Length: 237
Host: ufaclien
Content-Type: multipart/form-data; boundary=----------ThIs_Is_tHe_bouNdaRY_$
Connection: close
User-Agent: klive
------------ThIs_Is_tHe_bouNdaRY_$
Content-Disposition: form-data; name="file"; filename="testw.wps"
Content-Type: application/octet-stream
1328956550.991328956550.991328956550.991328956550.99
------------ThIs_Is_tHe_bouNdaRY_$--
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询