C# WebRequest 测试自动 登录58同城

.Net技术 码拜 6年前 (2015-05-08) 2243次浏览 0个评论

58同城登录比较复杂,包含了3个加密参数值,如下为 HttpWebRequest 模拟登录代码:

public void Login58(string name, string pass)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(“https://passport.58.com/login/”);
request.Method=”GET”;
request.CookieContainer = new CookieContainer();
HttpWebResponse response =(HttpWebResponse)request.GetResponse();
using(StreamReader str=new StreamReader(response.GetResponseStream()))
{
html = str.ReadToEnd();
}
cc = response.Cookies;
Match m = Regex.Match(html, “<input\\stype=\”hidden\”\\sname=\”path\”\\svalue=\”(?<path>[^\”]+)\”/>”, RegexOptions.Multiline | RegexOptions.IgnoreCase);
string path = HttpUtility.UrlEncode(m.Groups[“path”].Value);
string p1;
string p2;
string p3;
m = Regex.Match(html, “var\\stimespan\\s=\\s(?<timespan>\\d+)\\s-\\snew”, RegexOptions.Multiline | RegexOptions.IgnoreCase);
long timespan = long.Parse(m.Groups[“timespan”].Value);
timespan = timespan -long.Parse(wb1.InvokeScript(“getdatetime”).ToString())-60000;//假定是1分钟前加载的登录页面

long timesign = long.Parse(wb1.InvokeScript(“getdatetime”).ToString()) + timespan;
timesign = 1411094409520;
p1 = wb1.InvokeScript(“getp1”,new object[]{pass,timesign}).ToString();
p2 = wb1.InvokeScript(“getp2”, new object[] { pass, timesign }).ToString();
p3 = wb1.InvokeScript(“getp3”, new object[] { pass, timesign }).ToString();
m = Regex.Match(html, “<input\\stype=\”hidden\”\\sid=\”ptk\”\\sname=\”ptk\”\\sid=\”ptk\”\\svalue=\”(?<ptk>[^\”]+)\”/>”, RegexOptions.IgnoreCase | RegexOptions.Multiline);

string ptk = m.Groups[“ptk”].Value;

m = Regex.Match(html, “<input\\stype=\”hidden\”\\sid=\”cd\”\\sname=\”cd\”\\sid=\”cd\”\\svalue=\”(?<cd>[^\”]+)\”/>”, RegexOptions.IgnoreCase | RegexOptions.Multiline);
string cd = m.Groups[“cd”].Value;

string username = HttpUtility.UrlEncode(name);

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);

string postUrl = “https://passport.58.com/dounionlogin”;

HttpWebRequest postRequest = (HttpWebRequest)WebRequest.Create(postUrl);
postRequest.Method = “POST”;
postRequest.Referer = “https://passport.58.com/login/”;
postRequest.ContentType = ” application/x-www-form-urlencoded”;
postRequest.CookieContainer = new CookieContainer();
postRequest.CookieContainer.Add(cc);

byte[] postBytes = Encoding.UTF8.GetBytes(poststr);
using (Stream postDataStream = postRequest.GetRequestStream())
{
postDataStream.Write(postBytes, 0, postBytes.Length);
}
HttpWebResponse response1 = (HttpWebResponse)postRequest.GetResponse();

using (StreamReader sr = new StreamReader(response1.GetResponseStream()))
{
html = sr.ReadToEnd();
}
m = Regex.Match(html, “(?<num>\\d+)\””, RegexOptions.Multiline | RegexOptions.IgnoreCase);
string num = m.Groups[“num”].Value;
string cookies = response1.Headers[“Set-Cookie”];

Match m_www58com = Regex.Match(cookies, “(?<g_58com>www58com=\”.*?\”)”, RegexOptions.Multiline | RegexOptions.IgnoreCase);
Match m_58cooper = Regex.Match(cookies, “(?<g_58cooper>58cooper=\”.*?\”)”, RegexOptions.Multiline | RegexOptions.IgnoreCase);
Match m_ppu = Regex.Match(cookies, “(?<ppu>PPU=\”.*?\”)”, RegexOptions.Multiline | RegexOptions.IgnoreCase);
string s_www58com = m_www58com.Groups[“g_58com”].Value;
string s_58cooper = m_58cooper.Groups[“g_58cooper”].Value;
string s_ppu = m_ppu.Groups[“ppu”].Value;
string cook = “Cookie:” + s_58cooper + “;” + s_ppu + “;” + s_www58com;

request = (HttpWebRequest)WebRequest.Create(“http://my.58.com/?pts=” + num);
request.Referer = “http://passport.58.com/dounionlogin”;
request.Method = “GET”;
request.Headers.Add(cook);

response = (HttpWebResponse)request.GetResponse();
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
html = sr.ReadToEnd();
}
tbx.Text = html;
}
}


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C# WebRequest 测试自动 登录58同城
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!