测试自动登录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