![](/img/trans.png)
[英]Delphi - Can indy TIdTCPClient Send/Receive large streams at the same time
[英]Send file to website with Indy TIdTCPClient
我需要以正確的方式將帶有TIdTCPClient
的文件發送到top4top
網站。
我使用TIdTCPClient
的WriteFile()
選項發送它但它不起作用,並使用流發送但我收到一個錯誤的請求。
var
utf8: IIdTextEncoding;
sid,result: string;
lParam: TIdMultiPartFormDataStream;
begin
sid := 'Z2jAmKM%2CA8Ik2dJxlR9NlZUW65b';
if OpenDialog1.Execute then
begin
utf8 := IndyTextEncoding_UTF8;
lParam := TIdMultiPartFormDataStream.Create;
lParam.AddFormField('sid', sid);
lParam.AddFile('file_1_', OpenDialog1.FileName);
lParam.AddFormField('submitr', '[ رفع الملفات ]');
TCPC.host := 'up.top4top.net';
TCPC.Port := 443;
TCPC.ConnectTimeout := 100000;
TCPC.ReadTimeout := 500000;
TCPC.Connect;
TCPC.Socket.WriteLn('POST /index.php HTTP/1.1');
TCPC.Socket.WriteLn('Host: up.top4top.net');
TCPC.Socket.WriteLn('Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8');
TCPC.Socket.WriteLn('Accept-Encoding: gzip, deflate, br');
TCPC.Socket.WriteLn('Accept-Language: fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7');
TCPC.Socket.WriteLn('Content-Type: multipart/form-data; boundary=---- WebKitFormBoundarySSk63dIh0HIAto8S');
TCPC.Socket.WriteLn('DNT: 1');
TCPC.Socket.WriteLn('Origin: https://up.top4top.net');
TCPC.Socket.WriteLn('Referer: https://up.top4top.net/');
TCPC.Socket.WriteLn('Upgrade-Insecure-Requests: 1');
TCPC.Socket.WriteLn('User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 OPR/57.0.3098.106');
TCPC.Socket.Write(lParam);
TCPC.Socket.WriteLn('');
Result := TCPC.Socket.AllData(utf8);
TCPC.Disconnect;
end;
你犯了幾個錯誤:
您在Content-Type
標頭中指定的boundary
屬性的值與TIdMultiPartFormDataStream
在編碼其 MIME 數據時實際使用的值不匹配。 您需要使用TIdMultiPartFormDataStream.RequestContentType
屬性來正確設置Content-Type
標頭。 否則服務器將無法正確解析數據。
您根本沒有發送Content-Length
標頭。 需要告訴服務器您發送了多少數據。
您對WriteLn('')
的調用需要在對Write(lParam)
的調用之前,而不是在調用之后。 HTTP 消息的標頭和正文由空行分隔。
試試這個:
var
sid, result: string;
lParam: TIdMultiPartFormDataStream;
begin
sid := 'Z2jAmKM%2CA8Ik2dJxlR9NlZUW65b';
if OpenDialog1.Execute then
begin
lParam := TIdMultiPartFormDataStream.Create;
try
lParam.AddFormField('sid', sid);
lParam.AddFile('file_1_', OpenDialog1.FileName);
lParam.AddFormField('submitr', '[ رفع الملفات ]', 'utf-8');
TCPC.Host := 'up.top4top.net';
TCPC.Port := 443;
TCPC.ConnectTimeout := 100000;
TCPC.ReadTimeout := 500000;
// make sure you have an SSLIOHandler component
// assigned to the TCPC.IOHandler property, and
// its PassThrough property is set to False before
// sending any data...
TCPC.Connect;
try
// set PassThrough=False here if not already...
TCPC.Socket.WriteLn('POST /index.php HTTP/1.1');
TCPC.Socket.WriteLn('Host: up.top4top.net');
TCPC.Socket.WriteLn('Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8');
TCPC.Socket.WriteLn('Accept-Encoding: gzip, deflate, br');
TCPC.Socket.WriteLn('Accept-Language: fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7');
TCPC.Socket.WriteLn('Connection: close');
TCPC.Socket.WriteLn('Content-Type: ' + lParam.RequestContentType);
TCPC.Socket.WriteLn('Content-Length: ' + IntToStr(lParam.Size));
TCPC.Socket.WriteLn('DNT: 1');
TCPC.Socket.WriteLn('Origin: https://up.top4top.net');
TCPC.Socket.WriteLn('Referer: https://up.top4top.net/');
TCPC.Socket.WriteLn('Upgrade-Insecure-Requests: 1');
TCPC.Socket.WriteLn('User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 OPR/57.0.3098.106');
TCPC.Socket.WriteLn;
TCPC.Socket.Write(lParam);
Result := TCPC.Socket.AllData(IndyTextEncoding_UTF8);
finally
TCPC.Disconnect;
end;
finally
lParam.Free;
end;
end;
...
end;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.