久久精品精选,精品九九视频,www久久只有这里有精品,亚洲熟女乱色综合一区
    分享

    idHTTP最簡潔的修改和取得Cookie例子

     quasiceo 2017-02-15

    idHTTP最簡潔的修改和取得Cookie例子

     

    procedure TForm1.Button1Click(Sender: TObject);
    var
         HTTP: TidHTTP;
         html, s: string;
         i: integer;
    begin
         HTTP := TidHTTP.Create(nil);
         try
          HTTP.HandleRedirects := True;
          HTTP.AllowCookies := True;
          HTTP.Request.CustomHeaders.Values['Cookie'] := 'abcd';//修改Cookie 抓包可見
          html := HTTP.Get('http://www.baidu.com/');

          s := 'Cookies: ';
          if HTTP.CookieManager.CookieCollection.Count > 0 then
            for i := 0 to HTTP.CookieManager.CookieCollection.Count - 1 do
              s := s + HTTP.CookieManager.CookieCollection.Items[i].CookieText;
          Memo1.Lines.Add(s);//取得Cookie
         finally
          FreeAndNil(HTTP);
         end;
    end;
    //------------------------------------

    unit Unit1;

    interface

    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, IdCookieManager, IdBaseComponent, IdComponent, IdTCPConnection,
      IdTCPClient, IdHTTP;

    type
      TForm1 = class(TForm)
        IdHTTP1: TIdHTTP;
        IdCookieManager1: TIdCookieManager;
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;

    var
      Form1: TForm1;

    implementation

    {$R *.dfm}

    procedure TForm1.FormCreate(Sender: TObject);
    var
     Params: TStringList;
     HTML, loginurl, myuser: String;
     count,i:integer;
     _cookies, cookies:tstringlist;
     ll:boolean;
     name,value:String;

     procedure setcookies;
     var j:integer; s:string;
     begin
       count:=cookies.count;
       s:='';
       for j:=1 to count do
       begin
         IdCookieManager1.AddCookie(cookies[j-1],IdHTTP1.url.Host);
         s:=s+'; '+cookies[j-1];
       end;
       if s<>'' then
       begin
         delete(s,1,2);
         s:=s+';';
         IdHTTP1.Request.CustomHeaders.Values['Cookie']:=s;
         IdHTTP1.Request.RawHeaders.Values['Cookie']:=s;
         //('Cookie'+IdHTTP1.Request.RawHeaders.NameValueSeparator+s);
       end;{}
     end;

     procedure extractcookie(cookie:string; var name,value:string);
     var i,k:integer;
     begin
       i:=pos('=',cookie);
       k:=pos(';',cookie);
       if k=0 then k:=length(cookie);
       if i>0 then
       begin
         name:=copy(cookie,1,i-1);
         value:=copy(cookie,i+1,k-i-1);
       end      else
       begin
         name:='';
         value:='';
       end;
     end;

     procedure savecookies;
     var j:integer;
     begin
       count:=IdCookieManager1.CookieCollection.count;
       for j:=1 to count do
       begin
         extractcookie(IdCookieManager1.CookieCollection.Items[j-1].CookieText,name,value);
         cookies.Values[name]:=value;
       end;
    //   IdCookieManager1.CookieCollection.Clear;
     end;

     procedure saveit(name:string);
     begin
       with tfilestream.create(name,fmcreate) do
       try
         write(pansichar(html)^,length(html));
       finally
         free;
       end;
     end;

    begin
      ll:=false;
      loginurl:='http:///login';
      Params := TStringList.Create;
      try
        cookies:=tstringlist.Create;
    //    cookies.Duplicates:=dupIgnore;
    //    cookies.Sorted:=true;

        idhttp1.Host:='';
        html:=idhttp1.Get('http:///');// first get; get first cookie(s)
        savecookies;

        setcookies;
        html:=idhttp1.Get(loginUrl);// next get; this is clean: used for retrieving the viewstate
        savecookies;

        myuser:='crystyignat';
        Params.Values['userId'] := myuser;
        Params.Values['password'] := 'mypassword';
        Params.Values['op'] := 'login';

        IdHTTP1.HandleRedirects:=false;// now this made the buzz, because the cookies were not set when following the redirect
        try
          setcookies;
          HTML := IdHTTP1.Post(loginurl, Params);// now do the log in

          _Cookies := TStringList.Create;
          IdHTTP1.Response.RawHeaders.Extract('Set-cookie', _Cookies);
          for i := 0 to _Cookies.Count - 1 do
          begin
    //        IdCookieManager1.AddCookie(_Cookies[i], IdHTTP1.URL.Host);
            extractcookie(_Cookies[i],name,value);
            cookies.Values[name]:=value;
          end;
          _cookies.free;
    //      savecookies;

          if pos('<div class="welcome">Welcome, <b>'+myuser+'</b>',html)>0 then
          begin
            setCookies;
            html:=idhttp1.Get('http:///'); // software redirect
            savecookies;

            saveit('hhh.html');

    //        setCookies;
    //        html:=idhttp1.Get('http:///portal'); // another software redirect
            //savecookies;

            ll:=pos('<a class="tn" href="logout">log out',html)>0;
          end;
        except on e: EIdHTTPProtocolException do
        begin
          if e.ReplyErrorCode<>302 then
             raise e;
            // now this is the redirect
            count:=IdCookieManager1.CookieCollection.count;// get the next cookie (this will be the userid)
            for i:=1 to count do
              cookies.Add(IdCookieManager1.CookieCollection.Items[i-1].CookieText);

            setcookies;
            html:=idhttp1.Get(IdHTTP1.Response.Location);// follow redirect
          end;
        end;

        cookies.free;
      except on e: EIdHTTPProtocolException do
        begin
          showmessage(idHTTP1.response.ResponseText);
        end;
      end;
      Params.Free;
      showmessage('logged in? : '+booltostr(ll,true));
    end;

    end.

      本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發布,不代表本站觀點。請注意甄別內容中的聯系方式、誘導購買等信息,謹防詐騙。如發現有害或侵權內容,請點擊一鍵舉報。
      轉藏 分享 獻花(0

      0條評論

      發表

      請遵守用戶 評論公約

      類似文章 更多

      主站蜘蛛池模板: 久久99热只有频精品6狠狠| 精品国产精品中文字幕| 上课忘穿内裤被老师摸到高潮| 桃子视频在线播放WWW| 国产午夜亚洲精品国产成人 | A毛片终身免费观看网站| 欧美亚洲国产日韩一区二区 | 亚洲处破女AV日韩精品| 福利一区二区视频在线| 久久一日本道色综合久久| 无码人妻少妇久久中文字幕蜜桃| 美女被强奷到抽搐的动态图| 免费无码又爽又刺激高潮| 国产精品中文字幕自拍| 日韩欧美亚洲综合久久| 中文字幕日韩有码av| 精品国产一区二区三区2021| 最新中文字幕国产精品| 邻居少妇张开腿让我爽了一夜| 日韩高清国产中文字幕| 国产SUV精品一区二区四| 国产不卡av一区二区| 久久久久久国产精品免费免费男同| 日韩在线观看精品亚洲| 成人免费看片又大又黄| 日本一区二区三区专线| 色偷偷888欧美精品久久久 | 国产精品日日摸夜夜添夜夜添2021| 日日摸夜夜添狠狠添欧美| 人妻少妇偷人无码视频| 日韩系列精品无码免费不卡| 在线中文字幕亚洲日韩2020| 免费av深夜在线观看| 成A人片亚洲日本久久| 国产在线不卡精品网站 | 高清自拍亚洲精品二区| 亚洲AV综合色区无码另类小说| 婷婷色香五月综合缴缴情香蕉| 日韩欧国产精品一区综合无码| 国产精品点击进入在线影院高清| 2020国产欧洲精品网站|