Http Post with indy [英] Http Post with indy
本文介绍了Http Post with indy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码与印度,但不可靠,它不会工作,我无法弄清楚我没有做正确的事情。如何查看我在服务器上发送的是否有这样的工具?
procedure TForm1.btn1Click(Sender:TObject);
var
fname:string;
MS,dump:TMemoryStream;
http:TIdHTTP;
const
CRLF =#13#10;
begin
如果PromptForFileName(fname,'','','','',false)然后
begin
MS:= TMemoryStream.Create();
MS.LoadFromFile(fname);
dump:= TMemoryStream.Create();
http:= TIdHTTP.Create();
http.Request.ContentType:='multipart / form-data; boundary = ----------------------------- 7cf87224d2020a ';
fname:= CRLF +'----------------------------- 7cf87224d2020a'+ CRLF +'Content-Disposition:form -数据; name = \uploadedfile\; filename = \test.png'+ CRLF;
dump.Write(fname [1],Length(fname));
dump.Write(MS.Memory ^,MS.Size);
fname:= CRLF +'----------------------------- 7cf87224d2020a--'+ CRLF;
dump.Write(fname [1],Length(fname));
ShowMessage(IntToStr(dump.Size));
MS.Clear;
try
http.Request.Method:='POST';
http.Post('http://posttestserver.com/post.php',dump,MS);
ShowMessage(PAnsiChar(MS.Memory));
ShowMessage(IntToStr(http.ResponseCode));
除
ShowMessage('无法绑定套接字');
结束
结束
结束
解决方案
Indy有 TIdMultipartFormDataStream
为此目的:
程序TForm1.SendPostData;
var
Stream:TStringStream;
参数:TIdMultipartFormDataStream;
begin
Stream:= TStringStream.Create('');
try
参数:= TIdMultipartFormDataStream.Create;
try
Params.AddFile('File1','C:\test.txt','application / octet-stream');
try
HTTP.Post('http://posttestserver.com/post.php',Params,Stream);
除了
在E:异常do
ShowMessage('POST期间遇到错误'+ E.Message);
结束
ShowMessage(Stream.DataString);
finally
Params.Free;
结束
finally
Stream.Free;
结束
结束
I have a simple php script on my web server which I need to upload a file using HTTP POST, which I am doing in Delphi.
Here is my code with Indy but aparantely it won't work and I can't figure out what i am not doing properly. How can I view what I send on the server is there such a tool ?
procedure TForm1.btn1Click(Sender: TObject);
var
fname : string;
MS,dump : TMemoryStream;
http : TIdHTTP;
const
CRLF = #13#10;
begin
if PromptForFileName(fname,'','','','',false) then
begin
MS := TMemoryStream.Create();
MS.LoadFromFile(fname);
dump := TMemoryStream.Create();
http := TIdHTTP.Create();
http.Request.ContentType:='multipart/form-data;boundary =-----------------------------7cf87224d2020a';
fname := CRLF + '-----------------------------7cf87224d2020a' + CRLF + 'Content-Disposition: form-data; name=\"uploadedfile\";filename=\"test.png"' + CRLF;
dump.Write(fname[1],Length(fname));
dump.Write(MS.Memory^,MS.Size);
fname := CRLF + '-----------------------------7cf87224d2020a--' + CRLF;
dump.Write(fname[1],Length(fname));
ShowMessage(IntToStr(dump.Size));
MS.Clear;
try
http.Request.Method := 'POST';
http.Post('http://posttestserver.com/post.php',dump,MS);
ShowMessage(PAnsiChar(MS.Memory));
ShowMessage(IntToStr(http.ResponseCode));
except
ShowMessage('Could not bind socket');
end;
end;
end;
解决方案
Indy has TIdMultipartFormDataStream
for this purpose:
procedure TForm1.SendPostData;
var
Stream: TStringStream;
Params: TIdMultipartFormDataStream;
begin
Stream := TStringStream.Create('');
try
Params := TIdMultipartFormDataStream.Create;
try
Params.AddFile('File1', 'C:\test.txt','application/octet-stream');
try
HTTP.Post('http://posttestserver.com/post.php', Params, Stream);
except
on E: Exception do
ShowMessage('Error encountered during POST: ' + E.Message);
end;
ShowMessage(Stream.DataString);
finally
Params.Free;
end;
finally
Stream.Free;
end;
end;
这篇关于Http Post with indy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文