【问题标题】:How to correctly send POST request like there?如何正确发送 POST 请求?
【发布时间】:2015-12-22 12:44:11
【问题描述】:

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

正确回复的截图

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

我的回复有截图(不正确):

如您所见,所有字符串都排成一行。

有我的代码(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;

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

【问题讨论】:

  • 删除Data.Text:=URLDecode(Data.Text);

标签: delphi delphi-xe2 delphi-xe


【解决方案1】:

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;

【讨论】:

    猜你喜欢
    • 2016-07-03
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-19
    • 2015-08-07
    • 2012-07-04
    • 1970-01-01
    相关资源
    最近更新 更多