httpwebrequest保持cookie

之前还没有用WinForm操作过Cookie,为了保持Cookie,中间还出现了一点小小挫折,不过现在已经搞定了,把Cookie保持的代码贴给大家看看,有兴趣做类似工具的人可以参考一下。

       

  1 // Cookies集合保存
  2          public  CookieCollection CCol  =   null ;
  3
  4          // 设置公司代理
  5          public  WebProxy GetWP()
  6 ExpandedBlockStart.gifContractedBlock.gif         {
  7            WebProxy _WP = new WebProxy("h00proxy"80);
  8            _WP.BypassProxyOnLocal = true;
  9            NetworkCredential _CD = new NetworkCredential("davi xiong""asdfad""bqc");
 10            _WP.Credentials = _CD;
 11            return _WP;
 12        }

 13
 14          private   void  Login( string  strId,  string  strPassword)
 15 ExpandedBlockStart.gifContractedBlock.gif         {
 16
 17            ASCIIEncoding encodingA = new ASCIIEncoding();
 18
 19            CookieContainer myCookieContainer = new CookieContainer();
 20
 21            progressBar1.Value = 0;  // Process
 22            //=======GET================================================================================
 23            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://s1.travian.china.com/login.php");
 24            myRequest.Proxy = GetWP();
 25
 26            myRequest.CookieContainer = myCookieContainer;
 27
 28            progressBar1.Value = 10;  // Process
 29            HttpWebResponse myResponseA = (HttpWebResponse)myRequest.GetResponse();
 30            StreamReader readerA = new StreamReader(myResponseA.GetResponseStream(), Encoding.Default);
 31            string Getcontent = readerA.ReadToEnd();
 32
 33            progressBar1.Value = 20;  // Process
 34            Regex Reg = new Regex("<input type=\"hidden\" name=\"login\" value=\"(.*)\">");
 35            string login = "";
 36            if (Reg.IsMatch(Getcontent))
 37ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 38                Match Mc = Reg.Matches(Getcontent)[0];
 39                login = Mc.Groups[1].Value;
 40            }

 41            progressBar1.Value = 30;  // Process
 42
 43            Reg = new Regex("<input class=\"fm fm110\" type=\"text\" name=\"(.*)\" value=");
 44            string name = "";
 45            if (Reg.IsMatch(Getcontent))
 46ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 47                Match Mc = Reg.Matches(Getcontent)[0];
 48                name = Mc.Groups[1].Value;
 49            }

 50
 51            progressBar1.Value = 40;  // Process
 52            Reg = new Regex("<input class=\"fm fm110\" type=\"password\" name=\"(.*)\" value=");
 53            string pass = "";
 54            if (Reg.IsMatch(Getcontent))
 55ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 56                Match Mc = Reg.Matches(Getcontent)[0];
 57                pass = Mc.Groups[1].Value;
 58            }

 59
 60            progressBar1.Value = 50;  // Process
 61            Reg = new Regex("<p align=\"center\"><input type=\"hidden\" name=\"(.*)\" value=\"(.*)\">");
 62            string hid2name = "";
 63            string hid2value = "";
 64            if (Reg.IsMatch(Getcontent))
 65ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 66                Match Mc = Reg.Matches(Getcontent)[0];
 67                hid2name = Mc.Groups[1].Value;
 68                hid2value = Mc.Groups[2].Value;
 69            }

 70
 71            //=======DATA==========================================================
 72            progressBar1.Value = 60;  // Process
 73            ASCIIEncoding encoding = new ASCIIEncoding();
 74            string postData = name + "=" + strId;
 75            postData += "&" + pass + "=" + strPassword;
 76            postData += "&login=" + login;
 77            postData += "&autologin=ja&" + hid2name + "=" + hid2value + "&w=1024:768";
 78
 79            byte[] data = encoding.GetBytes(postData);
 80
 81            //=======POST================================================================================
 82            progressBar1.Value = 70;  // Process
 83            myRequest = (HttpWebRequest)WebRequest.Create("http://s1.travian.china.com/dorf1.php");
 84            myRequest.Proxy = GetWP();
 85
 86            myRequest.Method = "POST";
 87            myRequest.ContentType = "application/x-www-form-urlencoded";
 88            myRequest.ContentLength = data.Length;
 89            myRequest.CookieContainer = myCookieContainer; 
 90            Stream newStream = myRequest.GetRequestStream();
 91            newStream.Write(data, 0, data.Length);
 92            newStream.Close();
 93
 94            progressBar1.Value = 80;  // Process
 95            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
 96            CCol = myCookieContainer.GetCookies(myRequest.RequestUri);
 97            progressBar1.Value = 90;  // Process
 98            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
 99            string content = reader.ReadToEnd();
100
101            progressBar1.Value = 100;  // Process
102            progressBar1.Value = 0;  // Process
103        }

104
105          private   void  BtnLogin_Click( object  sender, EventArgs e)
106 ExpandedBlockStart.gifContractedBlock.gif        
107            Login("davi97""sffg");
108        }

109
110          // 获取登录后才能查看的网页数据
111          private   string  GetPageData( string  URL)
112 ExpandedBlockStart.gifContractedBlock.gif         {
113            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);
114            myRequest.Proxy = GetWP();
115
116            myRequest.CookieContainer = new CookieContainer();
117            myRequest.CookieContainer.Add(new Uri(URL), CCol);
118
119            HttpWebResponse myResponseA = (HttpWebResponse)myRequest.GetResponse();
120            StreamReader readerA = new StreamReader(myResponseA.GetResponseStream(), Encoding.Default);
121            return readerA.ReadToEnd();
122        }

123
124          private   void  BtnSearch_Click( object  sender, EventArgs e)
125 ExpandedBlockStart.gifContractedBlock.gif         {
126            string URL = "http://s1.travian.china.com/karte.php";
127            string ReqContent = GetPageData(URL);
128
129            //TODO : 自动搜索地图上的人员信息
130  
131 }

132
133

转载于:https://www.cnblogs.com/yidanda/archive/2009/07/22/1528530.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值