如何正确发送POST请求?

时间:2015-09-24 19:27:14

标签: delphi delphi-xe2 delphi-xe

如何正确发送这样的POST请求?

正确回复后的屏幕截图 enter image description here

你可以看到正确位置的所有线条。

我的回复有截图(不正确): enter image description here

如您所见,所有字符串排列在一行中。

有我的代码(Delphi):

 procedure TForm1.sButton1Click(Sender: TObject);
var
  HTTP: TIdHTTP;
  Data : TStringList;
  l,p:string;
html:string;
begin
HTTP:=TIdHTTP.Create(self);
Data := TStringList.Create;

HTTP.HandleRedirects:=True;
HTTP.Request.Host:='192.168.0.111';
HTTP.Request.Connection:='keep-alive';
HTTP.Request.CacheControl:='max-age=0';
HTTP.Request.Accept:='text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8';
HTTP.Request.UserAgent:='Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36';
HTTP.Request.AcceptLanguage:='ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4';
//-----------
HTTP.Request.Referer:='http://192.168.0.111/';
HTTP.Request.CustomHeaders.Clear;
  with HTTP.Request.CustomHeaders do
  begin
    AddValue('Cookie','Authorization=Basic YWRtaW46YWRtaW4=');
  end;
http.Get('http://192.168.0.111/');
HTTP.Request.ContentType:='text/plain';
HTTP.Request.Accept:='text/plain';
Data.Add('[WAN_ETH_INTF#2,0,0,0,0,0#0,0,0,0,0,0]0,2');
Data.Add('enable');
Data.Add('X_TP_lastUsedIntf');
Data.Add('[WAN_IP_CONN#2,1,1,0,0,0#0,0,0,0,0,0]1,0');
Data.Add('[WAN_IP_CONN#2,1,2,0,0,0#0,0,0,0,0,0]2,0');
Data.Add('[WAN_PPP_CONN#2,1,1,0,0,0#0,0,0,0,0,0]3,0');
Data.Add('[WAN_IP6_CONN#2,1,1,0,0,0#0,0,0,0,0,0]4,0');
Data.Text:=URLDecode(Data.Text);
html:=HTTP.Post('http://192.168.0.111/cgi?1&1&1&1&1',Data);
sMemo1.Lines.Add(html);;
Data.Free;
FreeAndNil(HTTP);
end;

如何在我的情况下做出正确的回应?

1 个答案:

答案 0 :(得分:10)

URLDecode()完全错了。摆脱它。

话虽如此,您使用的是TIdHTTP.Post()版本,用于以application/x-www-form-urlencoded格式提交HTML网络表单。 Post()会相应地对TStringList数据进行编码。这就是您在屏幕截图中看到的内容,但在这种情况下,这不是您想要的。要按字母顺序发送字符串数据,换行符等,您需要将数据保存到TStream,然后再保存到Post()。它将按原样传输,无需任何编码。

此外,此代码看起来也是错误的:

with HTTP.Request.CustomHeaders do
begin
  AddValue('Cookie','Authorization=Basic YWRtaW46YWRtaW4=');
end;

您确定应该发送Cookie标头吗?看起来更像是Authentication标题:

AddValue('Authorization', 'Basic YWRtaW46YWRtaW4=');

如果是,TIdHTTP内置支持Basic身份验证,则您不应该CustomHeaders使用Authentication: Basic ...标头:

HTTP.Request.Username := 'admin';
HTTP.Request.Password := 'admin';
HTTP.Request.BasicAuthentication := True;

请改为尝试:

var
  HTTP: TIdHTTP;
  Data: TMemoryStream;
  html:string;
begin
  HTTP := TIdHTTP.Create(nil);
  try
    HTTP.HandleRedirects := True;
    HTTP.Request.Connection := 'keep-alive';
    HTTP.Request.CacheControl := 'max-age=0';
    HTTP.Request.Accept := 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8';
    HTTP.Request.UserAgent := 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36';
    HTTP.Request.AcceptLanguage := 'ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4';
    //-----------
    HTTP.Request.Referer := 'http://192.168.0.111/';

    //HTTP.Request.CustomHeaders.AddValue('Cookie','Authorization=Basic YWRtaW46YWRtaW4=');
    HTTP.Request.Username := 'admin';
    HTTP.Request.Password := 'admin';
    HTTP.Request.BasicAuthentication := True;

    // you are calling the version of TIdHTTP.Get() that returns
    // the response data as a String, but you are ignoring that
    // data in this first request. You can optionally reduce some
    // memory usage and increase performance by telling Get() that
    // you do not want any response data returned to you. TIdHTTP
    // will still read it but silently discard it...
    //
    // HTTP.Get('http://192.168.0.111/', TStream(nil));
    HTTP.Get('http://192.168.0.111/');

    Data := TMemoryStream.Create;
    try
      HTTP.Request.ContentType := 'text/plain';
      HTTP.Request.Accept := 'text/plain';
      WriteStringToStream(Data, '[WAN_ETH_INTF#2,0,0,0,0,0#0,0,0,0,0,0]0,2'+EOL);
      WriteStringToStream(Data, 'enable'+EOL);
      WriteStringToStream(Data, 'X_TP_lastUsedIntf'+EOL);
      WriteStringToStream(Data, '[WAN_IP_CONN#2,1,1,0,0,0#0,0,0,0,0,0]1,0'+EOL);
      WriteStringToStream(Data, '[WAN_IP_CONN#2,1,2,0,0,0#0,0,0,0,0,0]2,0'+EOL);
      WriteStringToStream(Data, '[WAN_PPP_CONN#2,1,1,0,0,0#0,0,0,0,0,0]3,0'+EOL);
      WriteStringToStream(Data, '[WAN_IP6_CONN#2,1,1,0,0,0#0,0,0,0,0,0]4,0'+EOL);
      Data.Position := 0;
      html := HTTP.Post('http://192.168.0.111/cgi?1&1&1&1&1', Data);
      sMemo1.Lines.Add(html);
    finally
      Data.Free;
    end;
  finally
    HTTP.Free;
  end;
end;