测试自动登录58

 1  public void Login58(string name, string pass)
 2         {
 3             HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://passport.58.com/login/");
 4            request.Method="GET";
 5            request.CookieContainer = new CookieContainer();
 6             HttpWebResponse response =(HttpWebResponse)request.GetResponse();
 7             using(StreamReader str=new StreamReader(response.GetResponseStream()))
 8             {
 9                 html = str.ReadToEnd();
10             }
11             cc = response.Cookies;
12             Match m = Regex.Match(html, "<input\\stype=\"hidden\"\\sname=\"path\"\\svalue=\"(?<path>[^\"]+)\"/>", RegexOptions.Multiline | RegexOptions.IgnoreCase);
13             string path = HttpUtility.UrlEncode(m.Groups["path"].Value);
14             string p1;
15             string p2;
16             string p3;
17             m = Regex.Match(html, "var\\stimespan\\s=\\s(?<timespan>\\d+)\\s-\\snew", RegexOptions.Multiline | RegexOptions.IgnoreCase);
18             long timespan = long.Parse(m.Groups["timespan"].Value);
19             timespan = timespan -long.Parse(wb1.InvokeScript("getdatetime").ToString())-60000;//假定是1分钟前加载的登录页面
20 
21             long timesign = long.Parse(wb1.InvokeScript("getdatetime").ToString()) + timespan;
22             timesign = 1411094409520;
23             p1 = wb1.InvokeScript("getp1",new object[]{pass,timesign}).ToString();
24             p2 = wb1.InvokeScript("getp2", new object[] { pass, timesign }).ToString();
25             p3 = wb1.InvokeScript("getp3", new object[] { pass, timesign }).ToString();
26             m = Regex.Match(html, "<input\\stype=\"hidden\"\\sid=\"ptk\"\\sname=\"ptk\"\\sid=\"ptk\"\\svalue=\"(?<ptk>[^\"]+)\"/>", RegexOptions.IgnoreCase | RegexOptions.Multiline);
27 
28             string ptk = m.Groups["ptk"].Value;
29 
30             m = Regex.Match(html, "<input\\stype=\"hidden\"\\sid=\"cd\"\\sname=\"cd\"\\sid=\"cd\"\\svalue=\"(?<cd>[^\"]+)\"/>", RegexOptions.IgnoreCase | RegexOptions.Multiline);
31             string cd = m.Groups["cd"].Value;
32 
33             string username = HttpUtility.UrlEncode(name);
34 
35             string poststr = string.Format("isweak=0&path={0}&p1={1}&p2={2}&p3={3}&timesign={4}&ptk={5}&cd={6}&username={7}&password=password&remember=on&mcresult=undefined", path, p1, p2,p3,timesign, ptk, cd, username);
36             
37             string postUrl = "https://passport.58.com/dounionlogin";
38 
39             HttpWebRequest postRequest = (HttpWebRequest)WebRequest.Create(postUrl);
40             postRequest.Method = "POST";
41             postRequest.Referer = "https://passport.58.com/login/";
42             postRequest.ContentType = " application/x-www-form-urlencoded";
43             postRequest.CookieContainer = new CookieContainer();
44             postRequest.CookieContainer.Add(cc);
45 
46             byte[] postBytes = Encoding.UTF8.GetBytes(poststr);
47             using (Stream postDataStream = postRequest.GetRequestStream())
48             {
49                 postDataStream.Write(postBytes, 0, postBytes.Length);
50             }
51             HttpWebResponse response1 = (HttpWebResponse)postRequest.GetResponse();
52 
53             using (StreamReader sr = new StreamReader(response1.GetResponseStream()))
54             {
55                 html = sr.ReadToEnd();
56             }
57             m = Regex.Match(html, "(?<num>\\d+)\"", RegexOptions.Multiline | RegexOptions.IgnoreCase);
58             string num = m.Groups["num"].Value;           
59             string cookies = response1.Headers["Set-Cookie"];
60 
61             Match m_www58com = Regex.Match(cookies, "(?<g_58com>www58com=\".*?\")", RegexOptions.Multiline | RegexOptions.IgnoreCase);
62             Match m_58cooper = Regex.Match(cookies, "(?<g_58cooper>58cooper=\".*?\")", RegexOptions.Multiline | RegexOptions.IgnoreCase);
63             Match m_ppu = Regex.Match(cookies, "(?<ppu>PPU=\".*?\")", RegexOptions.Multiline | RegexOptions.IgnoreCase);
64             string s_www58com = m_www58com.Groups["g_58com"].Value;
65             string s_58cooper = m_58cooper.Groups["g_58cooper"].Value;
66             string s_ppu = m_ppu.Groups["ppu"].Value;
67             string cook = "Cookie:" + s_58cooper + ";" + s_ppu + ";" + s_www58com;
68 
69             request = (HttpWebRequest)WebRequest.Create("http://my.58.com/?pts=" + num);
70             request.Referer = "http://passport.58.com/dounionlogin";
71             request.Method = "GET";
72             request.Headers.Add(cook);
73 
74             response = (HttpWebResponse)request.GetResponse();
75             using (StreamReader sr = new StreamReader(response.GetResponseStream()))
76             {
77                 html = sr.ReadToEnd();
78             }
79             tbx.Text = html;
80         }
81     }

 

转载于:https://www.cnblogs.com/sekon/p/4216127.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值