热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

delphi7中idhttp接收与提交cookie的问题

下面是简单的介绍:首先在(通过idHttp的CookieManager),剩下的你就专心实现你要完成的东西就可以啦。不知道你

下面是简单的介绍:

首先在

(通过idHttp的COOKIEManager),剩下的 你就专心实现你要完成

的东西就可以啦。

不知道你玩过KOK没有,下面的代码是登陆KOK注册页面的部分代码:

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls, ExtCtrls, IdCOOKIEManager, IdBaseComponent,

IdComponent, IdTCPConnection, IdTCPClient, IdHTTP;

type

TForm1
= class(TForm)

http: TIdHTTP;

COOKIEMngr: TIdCOOKIEManager;

edtUserName: TLabeledEdit;

edtPassword: TLabeledEdit;

btnLogin: TButton;

COOKIEs: TMemo;

Memo1: TMemo;

btnInfor: TButton;

Button1: TButton;

edtSN: TLabeledEdit;

procedure btnLoginClick(Sender: TObject);

procedure btnInforClick(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

implementation

{$R *.dfm}

//这个函数是登陆页面,

procedure TForm1.btnLoginClick(Sender: TObject);

var

s, s1: TStringStream;

i: Integer;

begin

s :
= TStringStream.Create('');

s1 :
= TStringStream.Create('');

try

//{

s.WriteString('action=LOGIN&gameSelect=gkk&');

s.WriteString('acct=' + edtUserName.Text + '&');

s.WriteString('pwd=' + edtPassword.Text);

http.Request.ContentType := 'application/x-www-form-urlencoded';

try

http.

except

http.Get(http.Response.Location, s1);

end;

//
}

Memo1.Lines.Text :
= s1.DataString;

//下面的是显示COOKIEs信息的代码

COOKIEs.Clear;

for i := 0 to COOKIEMngr.COOKIECollection.Count - 1 do

COOKIEs.Lines.Add(COOKIEMngr.COOKIECollection.Items[i].COOKIEText);

finally

s.Free;

s1.Free;

end;

end;

//这是KOK注册页面中,显示帐户信息的函数

procedure TForm1.btnInforClick(Sender: TObject);

var

s, s1: TStringStream;

i: Integer;

begin

{








}

s :
= TStringStream.Create('');

s1 :
= TStringStream.Create('');

try

s.WriteString(
'action=INFO');

http.Request.ContentType :
= 'application/x-www-form-urlencoded';

try

http.Post(
'http://register.kok.com.cn/billing/servlet/walletServlet', s, s1)

except

http.Get(http.Response.Location, s1);

end;

Memo1.Lines.Text :
= s1.DataString;

COOKIEs.Clear;

for i := 0 to COOKIEMngr.COOKIECollection.Count - 1 do

COOKIEs.Lines.Add(COOKIEMngr.COOKIECollection.Items[i].COOKIEText);

finally

s.Free;

s1.Free;

end;

end;

end.

--------------------------------------------------------------------------------

procedure TForm1.Button1Click(Sender: TObject);

const

DFW_LOGIN_URL
= 'http://www.delphibbs.com/delphibbs/chkuser.asp';

UserName
= '你的用户名';

Password
= '你的密码'; //晕,刚才把密码写出来了,得改一下了

var

Params: TStrings;

HTML: String;

begin

Params:
=TStringList.Create;

try

Params.Add(
'URL='+'/delphibbs/collections.asp');

//登录成功后跳转到的URL,这里直接转到"我收藏的问题"

Params.Add(
'QUERY_STRING='); //登录成功后跳转URL的参数

Params.Add(
'txtName='+UserName);

Params.Add(
'txtPass='+Password);

//Params.Add('chkSave='); //是否记住我的密码

IdHttp.HandleRedirects:
=True;

HTML:
=IdHttp.Post(DFW_LOGIN_URL,Params);

if Pos('',HTML)>0 then

ShowMessage(
'登录失败!') //登录失败则用户名为空

else

ShowMessage(
'登录成功!');

//分析HTML,取出每一条收藏贴子的URL,下载保存

finally

Params.Free;

end;

end;

 

--------------------------------------------------------------------------------

搞了几天,终于有了点眉目,以下是我登录一个asp站点的delphi程序,使用了IDHttp控件和IDCOOKIEManager控件,在delphi7(带indy9)+win2k pro调试通过。

有不当之处请指正,如转载请注明作者:yannqi。

1、网站asp程序:

判断如果有COOKIE显示用户名和邮件;如果没有将获得的用户名和邮件写入COOKIE。

 

<%

if (request.COOKIEs("name")&#61;"" or request.COOKIEs("email")&#61;"") then

Response.COOKIEs(
"name") &#61; request("name")

Response.COOKIEs(
"email") &#61; request("email")

Response.write(request(
"name")&#43;","&#43;request("email")&#43;",写入COOKIE")

else

Response.write(
"Name:"&#43;request.COOKIEs("name"))

Response.write(
"
email:
"&#43;request.COOKIEs("email"))

end if

%>

 

2、delphi form

object Form1: TForm1

Left
&#61; 258

Top
&#61; 154

Width
&#61; 650

Height
&#61; 388

Caption
&#61; &#39;Form1&#39;

Color
&#61; clBtnFace

Font.Charset
&#61; DEFAULT_CHARSET

Font.Color
&#61; clWindowText

Font.Height
&#61; -11

Font.Name
&#61; &#39;MS Sans Serif&#39;

Font.Style
&#61; []

OldCreateOrder
&#61; False

PixelsPerInch
&#61; 96

TextHeight
&#61; 13

object btnLogin: TButton

Left
&#61; 256

Top
&#61; 24

Width
&#61; 75

Height
&#61; 25

Caption
&#61; &#39;提交&#39;

TabOrder
&#61; 0

OnClick
&#61; btnLoginClick

end

object edtUserName: TLabeledEdit

Left
&#61; 0

Top
&#61; 24

Width
&#61; 121

Height
&#61; 21

EditLabel.Width
&#61; 50

EditLabel.Height
&#61; 13

EditLabel.Caption
&#61; &#39;UserName&#39;

TabOrder
&#61; 1

Text
&#61; &#39;yannqi&#39;

end

object edtPassword: TLabeledEdit

Left
&#61; 128

Top
&#61; 24

Width
&#61; 121

Height
&#61; 21

EditLabel.Width
&#61; 25

EditLabel.Height
&#61; 13

EditLabel.Caption
&#61; &#39;Email&#39;

TabOrder
&#61; 2

Text
&#61; &#39;xayahe&#64;163.com&#39;

end

object Memo1: TMemo

Left
&#61; 312

Top
&#61; 64

Width
&#61; 321

Height
&#61; 281

Lines.Strings
&#61; (

&#39;Memo1&#39;)

TabOrder
&#61; 3

end

object COOKIEs: TMemo

Left
&#61; 8

Top
&#61; 64

Width
&#61; 297

Height
&#61; 281

Lines.Strings
&#61; (

&#39;COOKIEs&#39;)

TabOrder
&#61; 4

end

object btnInfor: TButton

Left
&#61; 336

Top
&#61; 24

Width
&#61; 75

Height
&#61; 25

Caption
&#61; &#39;测试&#39;

TabOrder
&#61; 5

OnClick
&#61; btnInforClick

end

object Button3: TButton

Left
&#61; 416

Top
&#61; 24

Width
&#61; 43

Height
&#61; 25

Caption
&#61; &#39;清空&#39;

TabOrder
&#61; 6

OnClick
&#61; Button3Click

end

object http: TIdHTTP

MaxLineAction
&#61; maException

ReadTimeout
&#61; 0

AllowCOOKIEs
&#61; False

ProxyParams.BasicAuthentication
&#61; False

ProxyParams.ProxyPort
&#61; 0

Request.ContentLength
&#61; -1

Request.ContentRangeEnd
&#61; 0

Request.ContentRangeStart
&#61; 0

Request.ContentType
&#61; &#39;text/html&#39;

Request.Accept
&#61; &#39;text/html, */*&#39;

Request.BasicAuthentication
&#61; False

Request.UserAgent
&#61; &#39;Mozilla/3.0 (compatible; Indy Library)&#39;

HTTPOptions
&#61; [hoForceEncodeParams]

COOKIEManager
&#61; COOKIEMngr

Left
&#61; 120

Top
&#61; 96

end

object COOKIEMngr: TIdCOOKIEManager

Left
&#61; 152

Top
&#61; 96

end

end

 

3、unit1。pas

 

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls, ExtCtrls, IdCOOKIEManager, IdBaseComponent,

IdComponent, IdTCPConnection, IdTCPClient, IdHTTP;

type

TForm1
&#61; class(TForm)

http: TIdHTTP;

COOKIEMngr: TIdCOOKIEManager;

edtUserName: TLabeledEdit;

edtPassword: TLabeledEdit;

btnLogin: TButton;

COOKIEs: TMemo;

Memo1: TMemo;

btnInfor: TButton;

Button3: TButton;

procedure btnLoginClick(Sender: TObject);

procedure btnInforClick(Sender: TObject);

procedure Button3Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnLoginClick(Sender: TObject);

var

s, s1: TStringStream;

i: Integer;

begin

s :
&#61; TStringStream.Create(&#39;&#39;);

s1 :
&#61; TStringStream.Create(&#39;&#39;);

try

s.WriteString(
&#39;name&#61;&#39; &#43; edtUserName.Text &#43; &#39;&&#39;);

s.WriteString(
&#39;email&#61;&#39; &#43; edtPassword.Text);

http.Request.ContentType :
&#61; &#39;application/x-www-form-urlencoded&#39;;

try

http.Post(
&#39;http://localhost/COOKIEtest.asp&#39;, s, s1)

except

http.Get(http.Response.Location, s1);

end;

//}

Memo1.Lines.Text :
&#61; s1.DataString;

//下面的是显示COOKIEs信息的代码

COOKIEs.Clear;

COOKIEs.Lines.Add(inttostr(COOKIEMngr.COOKIECollection.Count));

for i :&#61; 0 to COOKIEMngr.COOKIECollection.Count - 1 do

COOKIEs.Lines.Add(COOKIEMngr.COOKIECollection.Items[i].COOKIEText);

finally

s.Free;

s1.Free;

end;

end;

procedure TForm1.btnInforClick(Sender: TObject);

var

s, s1: TStringStream;

i: Integer;

begin

s :
&#61; TStringStream.Create(&#39;&#39;);

s1 :
&#61; TStringStream.Create(&#39;&#39;);

try

http.Request.ContentType :
&#61; &#39;application/x-www&#39;;

http.AllowCOOKIEs:
&#61;true;

try

http.Post(
&#39;http://localhost/COOKIEtest.asp&#39;, s, s1)

except

http.Get(http.Response.Location, s1);

end;

Memo1.Lines.Text :
&#61; s1.DataString;

COOKIEs.Clear;

for i :&#61; 0 to COOKIEMngr.COOKIECollection.Count - 1 do

COOKIEs.Lines.Add(COOKIEMngr.COOKIECollection.Items[i].COOKIEText);

finally

s.Free;

s1.Free;

end;

end;

procedure TForm1.Button3Click(Sender: TObject);

begin

COOKIEs.Clear;

Memo1.Clear;

end;

end.

转:https://www.cnblogs.com/dwbboy/archive/2011/02/22/1961766.html



推荐阅读
author-avatar
伊倓
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有