Ins*_* FX 2 delphi delphi-xe delphi-xe2
如何正确发送这样的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;
Run Code Online (Sandbox Code Playgroud)
如何在我的情况下做出正确的回应?
Rem*_*eau 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;
Run Code Online (Sandbox Code Playgroud)
你确定你应该发送Cookie
标题吗?看起来更像是一个Authentication
标题而不是:
AddValue('Authorization', 'Basic YWRtaW46YWRtaW4=');
Run Code Online (Sandbox Code Playgroud)
如果是这样,TIdHTTP
还内置了支持Basic
认证,你不应该使用CustomHeaders
的Authentication: Basic ...
头都:
HTTP.Request.Username := 'admin';
HTTP.Request.Password := 'admin';
HTTP.Request.BasicAuthentication := True;
Run Code Online (Sandbox Code Playgroud)
试试这个:
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;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1701 次 |
最近记录: |