zoukankan      html  css  js  c++  java
  • dhttp与IdCookieManager处理登陆过程

    dhttp与IdCookieManager处理登陆过程

     

    我们知道,用IE注册网页(象论坛)时,它能够自动找出相应的Cookie并提交给服务器,从而使用户不用重新登录就能够看到与他自己帐号有关的内容.这是怎么实现的呢?

    如果我用用IE的ACTIVEX控件TWebBrowser,这个问题是不用考虑的,它自己处理了.但是有些场合 TWebBrowser并不是上佳的选择,比如我们要从网页上取点内容下来,但是不用显示,这样也用WebBrowser的话程序就显得笨拙了.

    我今天用的是idHttp,据说拿它和IdCookieManager连起来用很好用,但是我没有弄懂.我用的是别的办法,整理如下:

    *取得与网址有关的Cookie
    用InternetGetCookie这个API,它在WinInet单元中
    有4个参数,第一个是URL,第二个设为nil,第三个指到一个变量BUFFER,存放Cookie的内容,第四个是Cookie的长度
    InternetGetCookie(PChar(Edit1.Text), nil, buf, Size)

    *给idHttp设置Cookie
    idHttp1.Request.CustomHeaders.Text := 'Cookie: ' + Memo1.Lines.Text;

    *取网页内容
    Memo1.Lines.Text := idHttp1.Get(Url);

    比较完整的代码如下:
    procedure TForm1.Button1Click(Sender: TObject);
    var
    buf: array[0..1023] of char;
    Size: DWord;
    begin
    if InternetGetCookie(PChar(Edit1.Text), PChar(Edit2.Text), buf, Size) then
    begin
    Memo1.Lines.Text := Buf;
    idHttp1.Request.CustomHeaders.Text := 'Cookie: ' + Memo1.Lines.Text;
    end
    else
    Memo1.Lines.Text := 'error!';
    end;

    procedure TForm1.Button2Click(Sender: TObject);
    begin
    Memo2.Lines.Text := IdHTTP1.Get(Edit1.Text);
    end;

    //提次窗体
    uses MSHTML;

    procedure TForm1.Button1Click(Sender: TObject);
    begin
    (((WebBrowser1.Document as IHTMLDocument2).body.all as
    IHTMLElementCollection).item('key', 0) as IHTMLInputElement
    ).value := 'zswang';

    //提交
    (((WebBrowser1.Document as IHTMLDocument2).body.all as
    IHTMLElementCollection).item('hlf', 0) as IHTMLFormElement
    ).submit;
    end;

    procedure TForm1.FormCreate(Sender: TObject);
    begin
    WebBrowser1.Navigate('http://search.csdn.net/search.asp');
    end;

    idhttp1.Request.RawHeaders.Values['Cookie']   :=   '这里是cookie的值';   // 

  • 相关阅读:
    re模块---正则表达式
    configparser 配置文件模块
    svn服务器配置
    python中的list的方法
    python正则表达式
    os模块
    高阶函数
    递归
    推导式
    [转]Java中的多线程你只要看这一篇就够了
  • 原文地址:https://www.cnblogs.com/bwdblogs/p/10495809.html
Copyright © 2011-2022 走看看