我在我的web服務器上有一個簡單的php腳本,我需要使用HTTP POST上傳文件,這是我在Delphi中做的。Http Post with indy
這是我與Indy的代碼,但aparantely它不會工作,我無法弄清楚我做得不好。我如何查看我在服務器上發送的內容是否有這樣的工具?
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;
「它不起作用」是調試某些時最不可用的短語。什麼不行?它做錯了什麼? –
@MasonWheeler如果我知道答案可能我沒有問這個問題。它不上傳到web服務器,這是我的猜測,即郵件標題格式不正確,但我不知道我做錯了什麼。 – opc0de
爲什麼是MS.Clear;在http.Post(URL,MS)之前調用? – ComputerSaysNo