如何正确发送这样的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;
针对我的情况如何做出正确的 react ?
最佳答案
URLDecode()
完全错误。摆脱它。
话虽这么说,您正在使用的 TIdHTTP.Post()
版本用于在 application/x-www-form-urlencoded
中提交 HTML Web 表单格式。 Post()
将对 TStringList
数据进行相应编码。这就是您在屏幕截图中看到的内容,但这不是您在这种情况下想要的。要按原样发送字符串数据、换行符等,您需要将数据保存到 TStream
中,然后再 Post()
保存。它将按原样传输,不进行任何编码。
此外,这段代码看起来也有可疑的错误:
with HTTP.Request.CustomHeaders do
begin
AddValue('Cookie','Authorization=Basic YWRtaW46YWRtaW4=');
end;
您确定要发送 Cookie
header 吗?看起来更像是一个 Authentication
header :
AddValue('Authorization', 'Basic YWRtaW46YWRtaW4=');
如果是这样,TIdHTTP
内置了对 Basic
身份验证的支持,您不应使用 CustomHeaders
进行 身份验证:基本...
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;
关于delphi - 如何像那里一样正确发送 POST 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32769381/