如何像那里一样正确发送 POST 请求?
How to correctly send POST request like there?
如何正确发送这样的POST请求?
正确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;
我的情况如何做出正确的反应?
URLDecode()
完全错误。摆脱它。
也就是说,您使用的是 TIdHTTP.Post()
版本,用于提交 application/x-www-form-urlencoded
格式的 HTML 网络表单。 Post()
将相应地对 TStringList
数据进行编码。这就是您在屏幕截图中看到的,但这不是您在这种情况下想要的。要发送字符串数据 as-is、换行符和所有数据,您需要将数据保存到 TStream
,然后再保存到 Post()
。它将被传输as-is而不进行任何编码。
此外,这段代码看起来也有可疑的错误:
with HTTP.Request.CustomHeaders do
begin
AddValue('Cookie','Authorization=Basic YWRtaW46YWRtaW4=');
end;
您确定要发送 Cookie
header 吗?看起来更像是 Authentication
header 而不是:
AddValue('Authorization', 'Basic YWRtaW46YWRtaW4=');
如果是这样,TIdHTTP
built-in 支持 Basic
身份验证,您不应将 CustomHeaders
用于 Authentication: Basic ...
header全部:
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;
如何正确发送这样的POST请求?
正确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;
我的情况如何做出正确的反应?
URLDecode()
完全错误。摆脱它。
也就是说,您使用的是 TIdHTTP.Post()
版本,用于提交 application/x-www-form-urlencoded
格式的 HTML 网络表单。 Post()
将相应地对 TStringList
数据进行编码。这就是您在屏幕截图中看到的,但这不是您在这种情况下想要的。要发送字符串数据 as-is、换行符和所有数据,您需要将数据保存到 TStream
,然后再保存到 Post()
。它将被传输as-is而不进行任何编码。
此外,这段代码看起来也有可疑的错误:
with HTTP.Request.CustomHeaders do
begin
AddValue('Cookie','Authorization=Basic YWRtaW46YWRtaW4=');
end;
您确定要发送 Cookie
header 吗?看起来更像是 Authentication
header 而不是:
AddValue('Authorization', 'Basic YWRtaW46YWRtaW4=');
如果是这样,TIdHTTP
built-in 支持 Basic
身份验证,您不应将 CustomHeaders
用于 Authentication: Basic ...
header全部:
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;