社区
网络通信/分布式开发
帖子详情
如何读取cookie?(包括建什么工程,如何测试,麻烦好事做到底)
四十岁开始写程序
2003-08-15 05:21:53
如何读取cookie?
(包括建什么工程,
如何测试,麻烦好事做到底)
...全文
195
3
打赏
收藏
如何读取cookie?(包括建什么工程,如何测试,麻烦好事做到底)
如何读取cookie? (包括建什么工程, 如何测试,麻烦好事做到底)
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
3 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
匪六哥
2003-12-22
打赏
举报
回复
搞了几天,终于有了点眉目,以下是我登录一个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
%>
2、delphi form
object Form1: TForm1
Left = 258
Top = 154
Width = 650
Height = 388
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object btnLogin: TButton
Left = 256
Top = 24
Width = 75
Height = 25
Caption = '提交'
TabOrder = 0
OnClick = btnLoginClick
end
object edtUserName: TLabeledEdit
Left = 0
Top = 24
Width = 121
Height = 21
EditLabel.Width = 50
EditLabel.Height = 13
EditLabel.Caption = 'UserName'
TabOrder = 1
Text = 'yannqi'
end
object edtPassword: TLabeledEdit
Left = 128
Top = 24
Width = 121
Height = 21
EditLabel.Width = 25
EditLabel.Height = 13
EditLabel.Caption = 'Email'
TabOrder = 2
Text = 'xayahe@163.com'
end
object Memo1: TMemo
Left = 312
Top = 64
Width = 321
Height = 281
Lines.Strings = (
'Memo1')
TabOrder = 3
end
object Cookies: TMemo
Left = 8
Top = 64
Width = 297
Height = 281
Lines.Strings = (
'Cookies')
TabOrder = 4
end
object btnInfor: TButton
Left = 336
Top = 24
Width = 75
Height = 25
Caption = '测试'
TabOrder = 5
OnClick = btnInforClick
end
object Button3: TButton
Left = 416
Top = 24
Width = 43
Height = 25
Caption = '清空'
TabOrder = 6
OnClick = Button3Click
end
object http: TIdHTTP
MaxLineAction = maException
ReadTimeout = 0
AllowCookies = False
ProxyParams.BasicAuthentication = False
ProxyParams.ProxyPort = 0
Request.ContentLength = -1
Request.ContentRangeEnd = 0
Request.ContentRangeStart = 0
Request.ContentType = 'text/html'
Request.Accept = 'text/html, */*'
Request.BasicAuthentication = False
Request.UserAgent = 'Mozilla/3.0 (compatible; Indy Library)'
HTTPOptions = [hoForceEncodeParams]
CookieManager = CookieMngr
Left = 120
Top = 96
end
object CookieMngr: TIdCookieManager
Left = 152
Top = 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 = 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 := TStringStream.Create('');
s1 := TStringStream.Create('');
try
s.WriteString('name=' + edtUserName.Text + '&');
s.WriteString('email=' + edtPassword.Text);
http.Request.ContentType := 'application/x-www-form-urlencoded';
try
http.Post('http://localhost/cookietest.asp', s, s1)
except
http.Get(http.Response.Location, s1);
end;
//}
Memo1.Lines.Text := s1.DataString;
//下面的是显示cookies信息的代码
Cookies.Clear;
Cookies.Lines.Add(inttostr(CookieMngr.CookieCollection.Count));
for i := 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 := TStringStream.Create('');
s1 := TStringStream.Create('');
try
http.Request.ContentType := 'application/x-www';
http.AllowCookies:=true;
try
http.Post('http://localhost/cookietest.asp', 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;
procedure TForm1.Button3Click(Sender: TObject);
begin
cookies.Clear;
Memo1.Clear;
end;
end.
四十岁开始写程序
2003-08-18
打赏
举报
回复
实际上我的问题不是网络编程的问题,运行也不能采用: http://<your server>/project1.dll/mycookie
方式。
我在IE工具条上做了一个toolbar,我想让上面的按钮click事件读本机的cookie ,
我的问题是如何用 delphi 写了个单机版的程序,读本机的cookie.
还请大侠赐教。
zousoft
2003-08-16
打赏
举报
回复
http协议是非常强大的, 但它有个缺陷, 就是不能记忆. 解决办法就是使用cookie.
cookie实际上是一个字符串, 它的格式是"Name=Value", 客户可以拒绝接受cookie, 所以要考虑周全, 不要过于倚赖它.
TWebResponse的SetCookieField()和TWebRequest的CookieFields属性可以分别用来存取cookie.
SetCookieField(Values: TStrings; const ADomain, APath: string; AExpires: TDateTime; ASecure: Boolean);
Values是cookie的实际内容, 可以包括多个cookie.
ADomain和APath用来限制cookie作用的服务器域和路径.
AExpires用于指定cookie的生命期, cookie将在此日期后作废. 一般采取Now+天数的格式. 也可以通过设置一个过去的日期, 使cookie无效.
ASecure一般设为False.
使用实例: 用cookie纪录上次访问时间和访问总次数.
New -> Others -> WebServerApplication -> ISAPI/NSAPI Dynamic Link Library
新建一个Action, PathInfo设为mycookie并在其OnAction中加入如下代码:
procedure TWebModule1.WebModule1WebActionItem1Action(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
tmp: TStringList;
begin
Response.Content := Format('You have visited my web for %s times and the last time is %s.',
[Request.CookieFields.Values['Count'], Request.CookieFields.Values['Time']]);
tmp := TStringList.Create;
tmp.Add(Format('Count=%d', [strtoint(Request.CookieFields.Values['Count'])+1]));
tmp.Add(Format('Time=%s', [FormatDateTime('yyyy/mm/dd hh:mm:ss', Now)]));
Response.SetCookieField(tmp, 'your server', '', Now+30, False);
tmp.Free;
Handled := True;
end;
编译运行: http://<your server>/project1.dll/mycookie
Cookie
必看实战精讲(大神必经之路)
Cookie
是存储在客户机的文本文件,它们保存了大量轨迹信息。在 Servlet 技术基础上,JSP 显然能够提供对 HTTP
cookie
的支持。 通常有三个步骤来识别回头客: 服务器脚本发送一系列
cookie
至浏览器。比如名字,...
前端面试宝典
现在所有支持JavaScript的浏览器都会只用这个策略,所谓同源是指:域名,协议,端口相同,有一个不一样就是非同源策略1、
Cookie
、LocalStorage 和 IndexDB 无法
读取
2、DOM 和 Js对象无法获得3、AJAX 请求不能发送。
测试
基本技巧与方法;
一些常用模块的
测试
用例 1、登录 2、添加 3、查询 4、删除 1、登录 ①用户名和密码都符合要求(格式上的要求) ②用户名和密码都不符合要求(格式上的要求) ③用户名符合要求,密码不符合要求(格式上的...
前端面试官常问的问题有哪些?(前端面试题总结)
对象Object(包含普通对象-Object,数组对象-Array,正则对象-RegExp,日期对象-Date,数学函数-Math,函数对象-Function) null和undefined 有什么区别? (1)null是一个表示”无”的对象,转我数值是为0,...
关于
测试
中常用到的一些方法、策略总结
一些常用模块的
测试
用例 1、登录 2、添加 3、查询 4、删除 1、登录 ①用户名和密码都符合要求(格式上的要求) ②用户名和密码都不符合要求(格式上的要求) ③用户名符合要求,密码不符合要求...
网络通信/分布式开发
1,593
社区成员
32,964
社区内容
发帖
与我相关
我的任务
网络通信/分布式开发
Delphi 网络通信/分布式开发
复制链接
扫一扫
分享
社区描述
Delphi 网络通信/分布式开发
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章