我想发送帖子请求如下所示程序是用delphi编写的。 请求标头中有一个cookie字段(红色矩形)。 我写了以下源代码。
procedure TForm1.Button2Click(Sender: TObject);
var
uri : TIdURI;
cookie : TIdCookieManager;
HTTP : TIdHTTP;
vals: TStringList;
url : String;
response : TStringStream;
begin
HTTP := TIdHTTP.Create();
HTTP.AllowCookies := True;
HTTP.Request.ContentType := 'application/x-www-form-urlencoded';
HTTP.HandleRedirects := True;
cookie := TIdCookieManager.Create();
uri := TIdURI.Create('www.hoge.com');
cookie.AddServerCookie('ASP.NET_SessionId=test', uri);
HTTP.CookieManager := cookie;
vals := TStringList.Create;
response := TStringStream.Create('');
vals.Add('__EVENTTARGET=');
vals.Add('__EVENTARGUMENT=');
vals.Add('__VIEWSTATE=/wEPDwUINzcxNjQyMjkPFgIeE1ZhbGlkYXRlUmVxdWVzdE1vZGUCAWRkEHyFbwQQE8tM5FKRr3UELd00osRNQBzu31XZl1yd86A=');
vals.Add('__VIEWSTATEGENERATOR=A7C0DD1C');
vals.Add('__EVENTVALIDATION=/wEdAAZkcRcs1jgA2rEUAtpu7qzIhFuNiVVNuLciluwM7Vty0gJCK50467l5FRCktGxHOlNKe/Y7d9SBufbGEp2w5OLHqFe59uEio+bAp/33YZOR3aKeKEbp39eHc9mbdvkCgxAPflO5NLAHc5uwdZn6JOnwKMi9h+dluqFLpmg3gO25cg==');
vals.Add('ddlLanguage=ja-JP');
vals.Add('tbUserId=myid');
vals.Add('tbPassword=hoge');
vals.Add('btnLogin=login');
url := TIdURI.ParamsEncode('ReturnUrl=/GssNet/main/default.aspx');
url := TIdURI.URLEncode('www.hoge.com/GssNet/login,aspx?ReturnUrl=/GssNet/main/default.aspx');
try
HTTP.Post('http://www.hoge.com/GssNet/login,aspx', vals, response);
Except
on EIdHTTPProtocolException do
begin
ShowMessage(Memo1.TextHint);
end;
end;
end;
但cookie字段未包含在请求标头中。以下是数据包捕获的结果。当我执行我的程序时。请教我添加cookie字段以请求标题的方法。
答案 0 :(得分:2)
正如@ smooty86在评论中所说,在手动添加cookie时,您需要包含URL的http://
部分。您还需要包含所请求资源的完整路径,否则该cookie仅对根/
路径的请求有效。
此外,由于您未使用编码的TIdURI.ParamsEncode()
变量,因此您对TIdURI.URLEncode()
和url
的来电无效。如果您打算花时间对URL进行编码,请确保实际使用它。
您也在泄漏所有物品。
请改为尝试:
procedure TForm1.Button2Click(Sender: TObject);
var
uri : TIdURI;
HTTP : TIdHTTP;
vals : TStringList;
url : String;
response : TStringStream;
begin
try
response := TStringStream.Create('');
try
vals := TStringList.Create;
try
vals.Add('__EVENTTARGET=');
vals.Add('__EVENTARGUMENT=');
vals.Add('__VIEWSTATE=/wEPDwUINzcxNjQyMjkPFgIeE1ZhbGlkYXRlUmVxdWVzdE1vZGUCAWRkEHyFbwQQE8tM5FKRr3UELd00osRNQBzu31XZl1yd86A=');
vals.Add('__VIEWSTATEGENERATOR=A7C0DD1C');
vals.Add('__EVENTVALIDATION=/wEdAAZkcRcs1jgA2rEUAtpu7qzIhFuNiVVNuLciluwM7Vty0gJCK50467l5FRCktGxHOlNKe/Y7d9SBufbGEp2w5OLHqFe59uEio+bAp/33YZOR3aKeKEbp39eHc9mbdvkCgxAPflO5NLAHc5uwdZn6JOnwKMi9h+dluqFLpmg3gO25cg==');
vals.Add('ddlLanguage=ja-JP');
vals.Add('tbUserId=myid');
vals.Add('tbPassword=hoge');
vals.Add('btnLogin=login');
HTTP := TIdHTTP.Create;
try
HTTP.HandleRedirects := True;
HTTP.AllowCookies := True;
HTTP.CookieManager := TIdCookieManager.Create(HTTP);
uri := TIdURI.Create('http://www.hoge.com/GssNet/login,aspx');
try
HTTP.CookieManager.AddServerCookie('ASP.NET_SessionId=test', uri);
finally
uri.Free;
end;
url := TIdURI.URLEncode('http://www.hoge.com/GssNet/login,aspx?ReturnUrl=/GssNet/main/default.aspx');
HTTP.Request.ContentType := 'application/x-www-form-urlencoded';
HTTP.Post(url, vals, response);
finally
HTTP.Free;
end;
finally
vals.Free;
end;
// use response as needed...
finally
response.Free;
end;
except
on EIdHTTPProtocolException do
begin
ShowMessage(Memo1.TextHint);
end;
end;
end;
最后,您为什么要向login,aspx
发送请求?正确的名称是login.aspx
。 <{1}}在服务器上不存在。