Delphi用IdHTTP提交自定义Cookie


本文整理自网络,侵删。

 

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.4936.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.4936.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值获取

相关阅读 >>

Delphi tmemo 可以显示、编辑多行文本

Delphi中操作olevariant、variant和stream

Delphi 得到cxgrid筛选后的记录数

Delphi 提升进程令牌

Delphi实现win10下Delphi 10.3.1 inline hook 调试器法获取寄存器并修改

Delphi 提取字符串左侧内容

Delphi base32 的加密和解密

Delphi工具之winsight

Delphi http post json示例

Delphi rect()

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...