西西软件园多重安全检测下载网站、值得信赖的软件下载站!
软件
软件
文章
搜索

首页编程开发Delphi → delphi7中使用idhttp接收与提交cookie可能会碰到的问题

delphi7中使用idhttp接收与提交cookie可能会碰到的问题

前往专题相关软件相关文章发表评论 来源:本站整理时间:2010/7/25 10:26:57字体大小:A-A+

作者:佚名点击:782次评论:0次标签: cookie

  • 类型:站长工具大小:5KB语言:中文 评分:7.5
  • 标签:
立即下载

下面是简单的介绍:

首先在

(通过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

{

<form action=../servlet/walletServlet method=post>

<input type=submit value="修改个人资料">

<input type=hidden

</form>

}

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('<USER Name="" />',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")="" or request.Cookies("email")="") then

Response.Cookies("name") = request("name")

Response.Cookies("email") = request("email")

Response.write(request("name")+","+request("email")+",写入cookie")

else

Response.write("Name:"+request.Cookies("name"))

Response.write("<br/>email:"+request.Cookies("email"))

end if

%>

    相关评论

    阅读本文后您有什么感想? 已有人给出评价!

    • 8 喜欢喜欢
    • 3 顶
    • 1 难过难过
    • 5 囧
    • 3 围观围观
    • 2 无聊无聊

    热门评论

    最新评论

    发表评论 查看所有评论(0)

    昵称:
    表情: 高兴 可 汗 我不要 害羞 好 下下下 送花 屎 亲亲
    字数: 0/500 (您的评论需要经过审核才能显示)