idHTTP最简洁的修改和取得Cookie例子

 

procedure TForm1.Button1Click(Sender: TObject);
var
HTTP: TidHTTP;
html, s: string;
i: integer;
begin
HTTP := TidHTTP.Create(nil);
try
HTTP.HandleRedirects := True;
HTTP.AllowCookies := True;
HTTP.Request.CustomHeaders.Values['Cookie'] := 'abcd';//修改Cookie 抓包可见
html := HTTP.Get('http://www.baidu.com/');

s := 'Cookies: ';
if HTTP.CookieManager.CookieCollection.Count > 0 then
for i := 0 to HTTP.CookieManager.CookieCollection.Count - 1 do
s := s + HTTP.CookieManager.CookieCollection.Items[i].CookieText;
Memo1.Lines.Add(s);//取得Cookie
finally
FreeAndNil(HTTP);
end;
end;
//------------------------------------

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdCookieManager, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, IdHTTP;

type
TForm1 = class(TForm)
IdHTTP1: TIdHTTP;
IdCookieManager1: TIdCookieManager;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
Params: TStringList;
HTML, loginurl, myuser: String;
count,i:integer;
_cookies, cookies:tstringlist;
ll:boolean;
name,value:String;

procedure setcookies;
var j:integer; s:string;
begin
count:=cookies.count;
s:='';
for j:=1 to count do
begin
IdCookieManager1.AddCookie(cookies[j-1],IdHTTP1.url.Host);
s:=s+'; '+cookies[j-1];
end;
if s<>'' then
begin
delete(s,1,2);
s:=s+';';
IdHTTP1.Request.CustomHeaders.Values['Cookie']:=s;
IdHTTP1.Request.RawHeaders.Values['Cookie']:=s;
//('Cookie'+IdHTTP1.Request.RawHeaders.NameValueSeparator+s);
end;{}
end;

procedure extractcookie(cookie:string; var name,value:string);
var i,k:integer;
begin
i:=pos('=',cookie);
k:=pos(';',cookie);
if k=0 then k:=length(cookie);
if i>0 then
begin
name:=copy(cookie,1,i-1);
value:=copy(cookie,i+1,k-i-1);
end else
begin
name:='';
value:='';
end;
end;

procedure savecookies;
var j:integer;
begin
count:=IdCookieManager1.CookieCollection.count;
for j:=1 to count do
begin
extractcookie(IdCookieManager1.CookieCollection.Items[j-1].CookieText,name,value);
cookies.Values[name]:=value;
end;
// IdCookieManager1.CookieCollection.Clear;
end;

procedure saveit(name:string);
begin
with tfilestream.create(name,fmcreate) do
try
write(pansichar(html)^,length(html));
finally
free;
end;
end;

begin
ll:=false;
loginurl:='http://feedmelinks.com/login';
Params := TStringList.Create;
try
cookies:=tstringlist.Create;
// cookies.Duplicates:=dupIgnore;
// cookies.Sorted:=true;

idhttp1.Host:='feedmelinks.com';
html:=idhttp1.Get('http://feedmelinks.com/');// first get; get first cookie(s)
savecookies;

setcookies;
html:=idhttp1.Get(loginUrl);// next get; this is clean: used for retrieving the viewstate
savecookies;

myuser:='crystyignat';
Params.Values['userId'] := myuser;
Params.Values['password'] := 'mypassword';
Params.Values['op'] := 'login';

IdHTTP1.HandleRedirects:=false;// now this made the buzz, because the cookies were not set when following the redirect
try
setcookies;
HTML := IdHTTP1.Post(loginurl, Params);// now do the log in

_Cookies := TStringList.Create;
IdHTTP1.Response.RawHeaders.Extract('Set-cookie', _Cookies);
for i := 0 to _Cookies.Count - 1 do
begin
// IdCookieManager1.AddCookie(_Cookies[i], IdHTTP1.URL.Host);
extractcookie(_Cookies[i],name,value);
cookies.Values[name]:=value;
end;
_cookies.free;
// savecookies;

if pos('<div class="welcome">Welcome, <b>'+myuser+'</b>',html)>0 then
begin
setCookies;
html:=idhttp1.Get('http://feedmelinks.com/'); // software redirect
savecookies;

saveit('hhh.html');

// setCookies;
// html:=idhttp1.Get('http://feedmelinks.com/portal'); // another software redirect
//savecookies;

ll:=pos('<a class="tn" href="logout">log out',html)>0;
end;
except on e: EIdHTTPProtocolException do
begin
if e.ReplyErrorCode<>302 then
raise e;
// now this is the redirect
count:=IdCookieManager1.CookieCollection.count;// get the next cookie (this will be the userid)
for i:=1 to count do
cookies.Add(IdCookieManager1.CookieCollection.Items[i-1].CookieText);

setcookies;
html:=idhttp1.Get(IdHTTP1.Response.Location);// follow redirect
end;
end;

cookies.free;
except on e: EIdHTTPProtocolException do
begin
showmessage(idHTTP1.response.ResponseText);
end;
end;
Params.Free;
showmessage('logged in? : '+booltostr(ll,true));
end;

end.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: rest idhttp是一种用于实现RESTful API的Delphi编程语言中的一个组件。RESTful API是一种使用HTTP协议进行通信的软件架构风格,它通过HTTP方法(如GET、POST、PUT和DELETE)与服务器进行交互,实现了客户端和服务器端之间的数据传输和操作。 idhttp是Delphi中的一个HTTP客户端组件,它内置了对HTTP协议的支持,并且提供了许多方法和属性,以便开发者可以轻松地发送HTTP请求和接收HTTP响应。在实现RESTful API时,我们可以使用idhttp组件来构建和发送HTTP请求,然后处理服务器端返回的数据。 使用rest idhttp可以实现许多常见的RESTful API操作,例如获取资源、创建资源、更新资源和删除资源等。我们可以使用idhttp的GET方法来获取服务器端返回的资源数据,使用POST方法来创建新的资源,使用PUT方法来更新现有资源,使用DELETE方法来删除资源。 此外,rest idhttp还提供了一些其他的功能,例如设置请求头部、设置请求参数、设置代理服务器、处理cookie等。它还支持SSL加密协议,可以在与安全的HTTPS站点进行通信时保护数据的安全性。 总之,rest idhttp是一个方便易用的Delphi组件,可以帮助开发者实现RESTful API的访问和操作。它提供了丰富的功能和灵活的接口,使得开发者可以轻松地构建和管理与服务器端的数据交互。 ### 回答2: TIdHTTP是一个强大的开源组件,它属于Delphi的Internet Direct (Indy) 套件。它用于在Delphi应用程序中实现HTTPHTTPS通信。TIdHTTP组件提供了许多功能,使得开发者能够轻松地进行HTTP请求和响应的处理。 在使用TIdHTTP组件时,首先需要创建一个TIdHTTP实例,然后使用其方法来发送HTTP请求,并获取服务器的响应。TIdHTTP组件支持GET、POST、PUT、DELETE等HTTP请求方法,可以设置请求头、请求参数等。它还支持设置超时时间、重定向处理、Cookie管理、压缩、代理等功能。通过TIdHTTP组件,开发者可以方便地获取网页内容、上传文件、下载文件等。 在使用TIdHTTP组件发送请求时,首先需要设置请求的URL和请求方式。例如,要发送一个GET请求,可以使用TIdHTTP的Get方法,并传入想要请求的URL作为参数。TIdHTTP会返回服务器返回的响应内容。类似地,可以使用Post、Put、Delete等方法来发送不同类型的请求。 除了基本的HTTP请求和响应处理功能,TIdHTTP还支持一些高级功能。例如,可以使用TIdHTTP的Request.Accept编写Accept请求头,用于指定所期望的响应内容类型。还可以使用TIdHTTP的Request.ContentType来指定请求的内容类型。通过使用TIdHTTP的Request.RawHeaders属性,可以获取服务器返回的原始响应头信息。另外,TIdHTTP还支持多线程请求处理,可以进行并发请求。 总之,TIdHTTP是一个功能强大的组件,提供了丰富的功能来支持HTTPHTTPS通信。无论是获取网页内容、上传文件,还是发送各种类型的HTTP请求,TIdHTTP都是一个非常有用的工具,能够方便地在Delphi应用程序中进行网络通信。 ### 回答3: REST是一种基于HTTP协议的软件架构风格,它的目标是实现系统之间的互操作性和可伸缩性。而idhttp则是Delphi编程语言中的一个组件,可以用来实现HTTP协议的通信功能。 rest idhttp是指在Delphi编程语言中使用idhttp组件来实现RESTful风格的HTTP通信。通过idhttp组件,我们可以使用各种HTTP方法(如GET、POST、PUT、DELETE等)来发送请求和接收响应。同时,idhttp组件还提供了丰富的功能,比如设置请求的header、参数、timeout等;处理HTTP响应的状态码、header、内容等。 使用idhttp组件实现RESTful通信的主要步骤如下: 1. 创建一个idhttp对象,并设置相关属性,如服务器地址、端口、代理等。 2. 根据需求选择HTTP方法,比如GET方法用于获取资源,POST方法用于提交数据等。 3. 根据实际情况设置请求的header、参数、内容等。 4. 调用idhttp的Get、Post、Put、Delete等方法发送请求,并获取响应。 5. 解析和处理HTTP响应,根据状态码、头部信息、内容等进行相应的操作。 使用rest idhttp可以很方便地与各种RESTful接口进行交互,实现数据的传输和交换。它可以在Delphi开发的应用程序中轻松地实现与服务器的通信,并进行数据的增删改查等操作。 总之,rest idhttp是指在Delphi编程语言中使用idhttp组件来实现RESTful风格的HTTP通信,通过该组件可以方便地与RESTful接口进行数据的交互和操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值