您现在的位置:首页 >> 网络通讯 >> Indy >> 内容

Delphi用IdHTTP提交自定义Cookie

时间:2011/9/3 15:47:24 点击:

  核心提示:procedurePost_init;begin http.Request.SetHeaders;//必须加上,才能完成下面的工作//http.Request.RawHeaders.Values['C...
procedure  Post_init;
begin
  http.Request.SetHeaders;//必须加上,才能完成下面的工作
  //http.Request.RawHeaders.Values['Cookie']:=_SESSION;//自定义Cookie
  //http.Request.CustomHeaders.Values['Cookie']:=Cookie; //也得加上,不然Cookie没有意义,研究了很多
  http.Http.Request.CustomHeaders.Text:='Cookie: '+Cookie;//最强悍的一个,上面两种方式有时候就是不行
  http.HandleRedirects:= true;
  http.Request.UserAgent := 'OS/1.0';//自定义客户端的签名,可以自定义
  http.request.ContentType:='application/x-www-form-urlencoded';//使用POST方式提交时候,必须加上
  http.Request.Referer :='http://www.test.cn/index.php';
  http.Request.Host:=www.test.cn;
end;

下面同时对IdHttp的访问方式进行简单的说明,还是看代码吧:
function Post(AURL: string; const ASource: TStrings): string; overload;
function Post(AURL: string; const ASource: TStream): string; overload;
function Post(AURL: string; const ASource: TIdMultiPartFormDataStream): string; overload;
procedure Post(AURL: string; const ASource: TStrings; const AResponseContent: TStream); overload;
procedure Post(AURL: string; const ASource: TStream; const AResponseContent: TStream); overload;
procedure Post(AURL: string; const ASource: TIdMultiPartFormDataStream; AResponseContent: TStream); overload;
这是IdHttp进行POST方式的几个函数,就是几个参数:
第一个为URL值,第二个为本地传送的参数,第三个为服务器返回的参数(没有第三个函数的时候,返回的为服务器的参数)。
我们可以在第三个参数上面进行做文章,并可以做文件下载,读取web文件了。

get的方式如下:
procedure Get(AURL: string; const AResponseContent: TStream); overload;
function Get(AURL: string): string; overload;

参数和POST方式类型,但是没有第二个参数。

下面是IdHttp的相关例子:
HTTP.Post('http://www.test.cn/index.php',Params,ret);
showmessage(UTF8Decode(ret.DataString)); //UTF8编码的网页的代码
Cookie:='';
for i:=0 to Http.CookieManager.CookieCollection.Count -1 do
begin
   if(Cookie='') then Cookie:=HTTP.CookieManager.CookieCollection[i].CookieText else   Cookie:=_session+'; '+HTTP.CookieManager.CookieCollection.Items[i].ClientCookie;
end; //Cookie值获取

作者:网络 来源:转载
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
本类推荐
  • 没有
本类固顶
  • 没有
  • 盒子文章(www.2ccc.com) © 2024 版权所有 All Rights Reserved.
  • 沪ICP备05001939号