Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sockets 我的post tidtcpclient中的错误在哪里?_Sockets_Delphi_Post_Connection_Tcpclient - Fatal编程技术网

Sockets 我的post tidtcpclient中的错误在哪里?

Sockets 我的post tidtcpclient中的错误在哪里?,sockets,delphi,post,connection,tcpclient,Sockets,Delphi,Post,Connection,Tcpclient,我收到一个错误的请求我的代码出了什么问题 procedure TForm1.Button1Click(Sender: TObject); begin TCPClient1.Host :='aavtrain.com'; TCPClient1.Port := 80; TCPClient1.ConnectTimeout := 10000; TCPClient1.OnConnected := TCPClient1Connected; TCPClient1.ReadTimeout :=

我收到一个错误的请求我的代码出了什么问题

procedure TForm1.Button1Click(Sender: TObject);
begin
  TCPClient1.Host :='aavtrain.com';
  TCPClient1.Port := 80;
  TCPClient1.ConnectTimeout := 10000;
  TCPClient1.OnConnected := TCPClient1Connected;
  TCPClient1.ReadTimeout := 5000;
  TCPClient1.Connect;
end;

procedure TForm1.TCPClient1Connected(Sender: TObject);
var
  s: string;
begin
  //
  TCPClient1.Socket.WriteLn('POST HTTP/1.1');
  TCPClient1.Socket.WriteLn(sLineBreak);
  IdTCPClient1.Socket.WriteLn('http://aavtrain.comindex.asp');
  IdTCPClient1.Socket.WriteLn(sLineBreak);
  TCPClient1.Socket.WriteLn('user_name=binary');
  TCPClient1.Socket.WriteLn('&password=12345');
  TCPClient1.Socket.WriteLn('&Submit=Submit');
  TCPClient1.Socket.WriteLn('&login=true');
  TCPClient1.Socket.WriteLn(sLineBreak);
  repeat
    s := TCPClient1.Socket.ReadLn('');
    Memo1.Lines.Add(s);
  until s.Contains('try again');
  TCPClient1.Disconnect;
end;
您的HTTP消息格式完全不正确,发送到服务器的每一行都是错误的

HTTP消息由三部分组成:一个请求/响应行,紧接着是头,然后是正文。标头和正文由单个CRLF CRLF序列分隔,但您在POST请求行之后发送一个CRLF CRLF序列。事实上,一般来说,您发送的换行符太多了

POST行本身缺少所请求资源的路径

您根本没有发送任何HTTP头。您正在请求HTTP 1.1,它需要主机头。并且,您不发送内容类型标题,以便服务器知道您发布的数据类型,也不发送内容长度标题,以便服务器知道您发布的数据量

消息正文本身的格式也不正确。您需要将webform值作为单行发送,而不是每个值单独发送一行

请尝试以下方法:

procedure TForm1.Button1Click(Sender: TObject);
var
  PostData, Response: string;
  Enc: IIdTextEncoding;
begin
  PostData := 'user_name=binary&password=12345&Submit=Submit&login=true';
  Enc := IndyTextEncoding_UTF8;
  //
  TCPClient1.Host := 'aavtrain.com';
  TCPClient1.Port := 80;
  TCPClient1.ConnectTimeout := 10000;
  TCPClient1.ReadTimeout := 5000;
  TCPClient1.Connect;
  try
    TCPClient1.Socket.WriteLn('POST /index.asp HTTP/1.1');
    TCPClient1.Socket.WriteLn('Host: aavtrain.com');
    TCPClient1.Socket.WriteLn('Content-Type: application/x-www-form-urlencoded; charset=utf-8');
    TCPClient1.Socket.WriteLn('Content-Length: ' + IntToStr(Enc.GetByteCount(PostData)));
    TCPClient1.Socket.WriteLn('Connection: close');
    TCPClient1.Socket.WriteLn;
    TCPClient1.Socket.Write(PostData, Enc);

    // the following is NOT the right way to read
    // an HTTP response. This is just an example.
    // I'll leave it as an exercise for you to
    // research and figure out the proper way.
    // I've posted pseudo code for this on
    // StackOverflow many times before...
    Response := TCPClient1.Socket.AllData;
  finally
    TCPClient1.Disconnect;
  end;
  Memo1.Text := Response;
end;

请阅读和相关的RFC,以及关于HTML Web表单提交的W3C规范,请参阅和,因为很明显,您不了解HTTP实际上是如何工作的。从头开始实现每件事并不是件小事,因为这是一个非常复杂和复杂的协议。

如果我是你,我会使用TIdHTTPinstance,因为它内置了post方法……请提供一个。为什么代码中有TCPClient1和IdTCPClient1?请不要显示假代码。我想了解它,我可以成功地使用idhttp和tnethttpclient,但我想了解如何使用idtcpclient