private LoginParam GetPararm(string html) { if (html == "") { return(null); } LoginParam param = new LoginParam(); var match = Regex.Match(html, "(?<=id=\"__VIEWSTATE\" value=\")(.+?)(?=\" />)", RegexOptions.Multiline | RegexOptions.IgnoreCase); param.__VIEWSTATE = match.Value; if (param.__VIEWSTATE == "") { return(null); } match = Regex.Match(html, "(?<=id=\"__VIEWSTATEGENERATOR\" value=\")(.+?)(?=\" />)", RegexOptions.Multiline | RegexOptions.IgnoreCase); param.__VIEWSTATEGENERATOR = match.Value; if (param.__VIEWSTATEGENERATOR == "") { return(null); } return(param); }
public string Login2() { try { var helper = new HttpHelper(); var str = "http://www.globalcompanions.com/"; var item = new HttpItem { URL = str, Allowautoredirect = true, AutoRedirectCookie = true }; var result = helper.GetHtml(item); if (result.StatusCode != HttpStatusCode.OK) { return("登录失败(" + result.StatusCode + ")"); } LoginParam pararm = GetPararm(result.Html); if (pararm == null) { return("登录失败(LoginParam failed)"); } pararm.ctl00_Header_cntrlLogin_txtBoxLogin = UserName; pararm.ctl00_Header_cntrlLogin_txtBoxPassword = Password; var header = new WebHeaderCollection(); header.Add("Accept-Language:zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2"); header.Add("Accept-Encoding:gzip,deflate"); item = new HttpItem { URL = "http://www.globalcompanions.com/default.aspx", Referer = "http://www.globalcompanions.com/", Allowautoredirect = false, AutoRedirectCookie = false, Host = "www.globalcompanions.com", Method = "POST", Header = header, Postdata = pararm.ToPostData(), Cookie = result.Cookie, Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", UserAgent = "Mozilla/5.0 (compatible;Windows NT 6.1; WOW64;Trident/6.0;MSIE 9.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.27 Safari/537.36", ContentType = "application/x-www-form-urlencoded" }; result = helper.GetHtml(item); item.URL = result.RedirectUrl; item.Cookie = result.Cookie; result = helper.GetHtml(item); if (result.StatusCode != HttpStatusCode.OK) { return("登录失败(" + result.StatusCode + ")"); } var loginResult = GetLoginResult(result.Html); if (loginResult.IndexOf("登录成功", StringComparison.Ordinal) >= 0) { IsLogined = true; Cookie = item.Cookie; if (result.Cookie == null) { return(loginResult); } if (result.Cookie == "") { return(loginResult); } Cookie = Cookie + ";" + result.Cookie; } return(loginResult); } catch (Exception exception) { return("登录失败:" + exception.Message); } }
public string Login() { try { var helper = new HttpHelper(); var str = "http://www.globalcompanions.com/"; var header = new WebHeaderCollection(); header.Add("Accept-Encoding:gzip,deflate"); var item = new HttpItem { URL = str, Allowautoredirect = true, AutoRedirectCookie = true }; var result = helper.GetHtml(item); if (result.StatusCode != HttpStatusCode.OK) { return("登录失败(" + result.StatusCode + ")"); } LoginParam pararm = GetPararm(result.Html); if (pararm == null) { return("登录失败(LoginParam failed)"); } pararm.ctl00_Header_cntrlLogin_txtBoxLogin = UserName; pararm.ctl00_Header_cntrlLogin_txtBoxPassword = Password; var loginParameters = pararm.ToPostData(); var pBuffers = Encoding.UTF8.GetBytes(loginParameters); var request = (HttpWebRequest)WebRequest.Create("http://www.globalcompanions.com/default.aspx"); request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36"; request.Referer = "http://www.globalcompanions.com/"; request.AllowAutoRedirect = true; request.Method = "Post"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = pBuffers.Length; var requestStream = request.GetRequestStream(); var c = new CookieContainer(); request.CookieContainer = c; requestStream.Write(pBuffers, 0, pBuffers.Length); requestStream.Close(); var requestStr = JsonConvert.SerializeObject(request, new JsonSerializerSettings { MaxDepth = 6 }); var response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode != HttpStatusCode.OK) { return("登录失败(" + response.StatusCode + ")"); } var cookies = response.Headers.Get("Set-Cookie"); //request = (HttpWebRequest)WebRequest.Create(url); //request.CookieContainer = new CookieContainer(); //request.CookieContainer.SetCookies(request.RequestUri, cookies); response = (HttpWebResponse)request.GetResponse(); var rStream = response.GetResponseStream(); var r = new StreamReader(rStream); var content = r.ReadToEnd(); var loginResult = GetLoginResult(content); if (loginResult.IndexOf("登录成功", StringComparison.Ordinal) >= 0) { IsLogined = true; Cookie = item.Cookie; if (result.Cookie == null) { return(loginResult); } if (result.Cookie == "") { return(loginResult); } Cookie = Cookie + ";" + result.Cookie; } return(loginResult); } catch (Exception exception) { return("登录失败:" + exception.Message); } }