使用Indy TIdTCPClient将文件发送到网站

时间:2018-12-30 16:41:43

标签: delphi tcpclient indy

我需要使用正确的方式将TIdTCPClient的文件发送到top4top网站。

我使用WriteFile()中的TIdTCPClient选项发送了它,但是它不起作用,并使用流发送,但是请求很糟糕。

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;

1 个答案:

答案 0 :(得分:2)

您犯了几个错误:

  1. 您在Content-Type标头中指定的TIdMultiPartFormDataStream属性的值与TIdMultiPartFormDataStream.RequestContentType对其MIME数据进行编码时实际使用的值不匹配。您需要使用Content-Type属性来正确设置Content-Length标头。否则,服务器将无法正确解析数据。

  2. 您根本没有发送WriteLn('')标头。需要告知服务器正在发送多少数据。

  3. 您对Write(lParam)的呼叫必须在对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; 的呼叫之前,而不是之后。 HTTP消息的标头和正文由空白行分隔。

尝试以下方法:

TextView textView = (TextView) findViewById(R.id.textView);