public HttpResult GetHtml(HttpItem objhttpItem, CookieContainer CookieContainer) { try { this._CookieContainer = CookieContainer; this.SetRequest(objhttpItem); } catch (Exception exception) { return new HttpResult { Cookie = "", Header = null, Html = exception.Message, StatusDescription = "配置参考时报错" }; } return this.GetHttpRequestData(objhttpItem); }
public HttpResult GetHtml(HttpItem objhttpItem) { try { lock (this) { this.SetRequest(objhttpItem); } } catch (Exception exception) { return new HttpResult { Cookie = "", Header = null, Html = exception.Message, StatusDescription = "配置参考时报错" }; //return null; } return this.GetHttpRequestData(objhttpItem); }
private void SetProxy(HttpItem objhttpItem) { if (!string.IsNullOrEmpty(objhttpItem.ProxyIp)) { WebProxy proxy; if (objhttpItem.ProxyIp.Contains(":")) { string[] strArray = objhttpItem.ProxyIp.Split(new char[] { ':' }); proxy = new WebProxy(strArray[0].Trim(), Convert.ToInt32(strArray[1].Trim())) { Credentials = new NetworkCredential(objhttpItem.ProxyUserName, objhttpItem.ProxyPwd) }; this.request.Proxy = proxy; } else { proxy = new WebProxy(objhttpItem.ProxyIp, false) { Credentials = new NetworkCredential(objhttpItem.ProxyUserName, objhttpItem.ProxyPwd) }; this.request.Proxy = proxy; } this.request.Credentials = CredentialCache.DefaultNetworkCredentials; } }
private void SetRequest(HttpItem objhttpItem) { this.SetCer(objhttpItem); if ((objhttpItem.Header != null) && (objhttpItem.Header.Count > 0)) { foreach (string str in objhttpItem.Header.AllKeys) { this.request.Headers.Add(str, objhttpItem.Header[str]); } } this.SetProxy(objhttpItem); this.request.Headers.Add("Cache-control", "no-cache"); this.request.Headers.Add("Accept-Language", "zh-cn"); this.request.Method = objhttpItem.Method; this.request.Timeout = objhttpItem.Timeout; this.request.ReadWriteTimeout = objhttpItem.ReadWriteTimeout; this.request.Accept = objhttpItem.Accept; this.request.ContentType = objhttpItem.ContentType; this.request.KeepAlive = objhttpItem.KeepAlive; this.request.UserAgent = objhttpItem.UserAgent; this.encoding = objhttpItem.Encoding; this.SetCookie(objhttpItem); this.request.Referer = objhttpItem.Referer; this.request.AllowAutoRedirect = objhttpItem.Allowautoredirect; this.SetPostData(objhttpItem); if (objhttpItem.Connectionlimit > 0) { this.request.ServicePoint.ConnectionLimit = objhttpItem.Connectionlimit; } }
private void SetPostData(HttpItem objhttpItem) { if (this.request.Method.Trim().ToLower().Contains("post")) { byte[] postdataByte = null; if (((objhttpItem.PostDataType == PostDataType.Byte) && (objhttpItem.PostdataByte != null)) && (objhttpItem.PostdataByte.Length > 0)) { postdataByte = objhttpItem.PostdataByte; } else if (!((objhttpItem.PostDataType != PostDataType.FilePath) || string.IsNullOrEmpty(objhttpItem.Postdata))) { StreamReader reader = new StreamReader(objhttpItem.Postdata, this.encoding); postdataByte = Encoding.Default.GetBytes(reader.ReadToEnd()); reader.Close(); } else if (!string.IsNullOrEmpty(objhttpItem.Postdata)) { postdataByte = Encoding.Default.GetBytes(objhttpItem.Postdata); } if (postdataByte != null) { this.request.ContentLength = postdataByte.Length; this.request.GetRequestStream().Write(postdataByte, 0, postdataByte.Length); } } }
private void SetCookie(HttpItem objhttpItem) { if (!string.IsNullOrEmpty(objhttpItem.Cookie)) { this.request.Headers[HttpRequestHeader.Cookie] = objhttpItem.Cookie; } else this.request.CookieContainer = _CookieContainer; if (objhttpItem.CookieCollection != null) { //this.request.CookieContainer = new CookieContainer(); this.request.CookieContainer = _CookieContainer; this.request.CookieContainer.Add(objhttpItem.CookieCollection); } }
private void SetCer(HttpItem objhttpItem) { if (!string.IsNullOrEmpty(objhttpItem.CerPath)) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(this.CheckValidationResult); this.request = (HttpWebRequest) WebRequest.Create(objhttpItem.URL); this.request.ClientCertificates.Add(new X509Certificate(objhttpItem.CerPath)); } else { this.request = (HttpWebRequest) WebRequest.Create(objhttpItem.URL); } }
private HttpResult GetHttpRequestData(HttpItem objhttpitem) { HttpResult result = new HttpResult(); try { using (this.response = (HttpWebResponse) this.request.GetResponse()) { result.Responseuri = response.ResponseUri.ToString(); result.StatusCode = this.response.StatusCode; result.StatusDescription = this.response.StatusDescription; result.Header = this.response.Headers; if (this.response.Cookies != null) { result.Cookie = GetCookiesStr(this._CookieContainer); //result.CookieCollection = this.response.Cookies; } else if (this.response.Headers["set-cookie"] != null) { result.Cookie = this.response.Headers["set-cookie"]; } MemoryStream memoryStream = new MemoryStream(); if ((this.response.ContentEncoding != null) && this.response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase)) { memoryStream = GetMemoryStream(new GZipStream(this.response.GetResponseStream(), CompressionMode.Decompress)); } else { memoryStream = GetMemoryStream(this.response.GetResponseStream()); } byte[] bytes = memoryStream.ToArray(); memoryStream.Close(); if (objhttpitem.ResultType == ResultType.Byte) { result.ResultByte = bytes; } if (this.encoding == null) { System.Text.RegularExpressions.Match match = Regex.Match(Encoding.Default.GetString(bytes), "<meta([^<]*)charset=([^<]*)[\"']", RegexOptions.IgnoreCase); string str = (match.Groups.Count > 1) ? match.Groups[2].Value.ToLower() : string.Empty; str = str.Replace("\"", "").Replace("'", "").Replace(";", "").Replace("iso-8859-1", "gbk"); if (str.Length > 2) { this.encoding = Encoding.GetEncoding(str.Trim()); } else if (string.IsNullOrEmpty(this.response.CharacterSet)) { this.encoding = Encoding.UTF8; } else { this.encoding = Encoding.GetEncoding(this.response.CharacterSet); } } result.Html = this.encoding.GetString(bytes); } } catch (WebException exception) { this.response = (HttpWebResponse) exception.Response; result.Html = exception.Message; if (this.response != null) { result.StatusCode = this.response.StatusCode; result.StatusDescription = this.response.StatusDescription; } } catch (Exception exception2) { result.Html = exception2.Message; } if (objhttpitem.IsToLower) { result.Html = result.Html.ToLower(); } return result; }
internal bool 验证登录() { ImportDataLog.WriteLog("cookie:" + _CookieStr); _CookieStr = FullWebBrowserCookie.GetCookieInternal(new Uri("https://www.jiaoyimao.com"), false); Utilities.HttpHelper http = new Utilities.HttpHelper(); HttpItem item = new HttpItem() { // URL = "https://www.jiaoyimao.com/", URL = "https://www.jiaoyimao.com/merchant/staff/index", Method = "get",//URL 可选项 默认为Get Cookie = _CookieStr,//字符串Cookie 可选项 Referer = "https://www.jiaoyimao.com/",//来源URL 可选项 UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)",//用户的浏览器类型,版本,操作系统 可选项有默认值 ContentType = "application/x-www-form-urlencoded",//返回类型 可选项有默认值 Allowautoredirect = true,//是否根据301跳转 可选项 KeepAlive = true, Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*", //CerPath = "d:\123.cer",//证书绝对路径 可选项不需要证书时可以不写这个参数 //Connectionlimit = 1024,//最大连接数 可选项 默认为1024 ProxyIp = "",//代理服务器ID 可选项 不需要代理 时可以不设置这三个参数 //ProxyPwd = "123456",//代理服务器密码 可选项 //ProxyUserName = "******",//代理服务器账户名 可选项 }; HttpResult result = http.GetHtml(item); string html = result.Html; //if (html.IndexOf("<div class=\"user-link\">您好") > 0) //ImportDataLog.WriteLog(html); if (html.IndexOf("商家管理后台") > 0) { IsLogin = true; return true; } else { IsLogin = false; return false; } }