/// <summary> /// 复制cookie /// </summary> /// <param name="onlySessionCookie">true:仅复制会话cookie false:复制非会话cookie null:全部cookie</param> /// <returns>List<Cookie></returns> public List <Cookie> GetCloneCookies(Boolean onlySessionCookie) { List <Cookie> lists = HttpCommon.GetHttpCookies(this.cookies, true); List <Cookie> list = new List <Cookie>(); if (onlySessionCookie) { foreach (Cookie m in lists) { if (m.ExpiryDate == null) { list.Add(m.Clone()); } } } else { foreach (Cookie m in lists) { if (m.ExpiryDate != null) { list.Add(m.Clone()); } } } return(list); }
/// <summary> /// GetHttpCookies /// </summary> /// <param name="cookies">List<Cookie></param> /// <param name="hasSecure">Boolean</param> /// <returns>List<Cookie></returns> public static List <Cookie> GetHttpCookies(List <Cookie> cookies, Boolean hasSecure) { HttpCommon.RefreshCookies(cookies); List <Cookie> list; if (hasSecure) { list = cookies; } else { list = new List <Cookie>(); foreach (Cookie m in cookies) { if (!m.Secure) { list.Add(m.Clone()); } } } return(list); }
/// <summary> /// 创建新的http(s)请求,重置除cookie、connectTimeout、readTimeout、userAgent外的所有设置 /// </summary> /// <param name="url">url地址请求</param> /// <param name="isHostnameVerifier">是否不确认主机名</param> /// <returns>HttpUtil</returns> public HttpUtil Create(String url, Boolean isHostnameVerifier) { this.ClearForm(); try { http = WebRequest.Create(url) as HttpWebRequest; isHttps = url.StartsWith("https", StringComparison.OrdinalIgnoreCase); if (isHostnameVerifier && isHttps) { HttpCommon.HostnameVerifier(); } this.http.Timeout = connectTimeout; this.http.ReadWriteTimeout = readTimeout; this.http.UserAgent = userAgent; this.http.ContentType = "application/x-www-form-urlencoded"; this.http.Headers["Accept-Charset"] = "utf-8"; this.http.Method = "GET"; } catch { } return(this); }
/// <summary> /// 连接并返回网页流 /// </summary> /// <param name="charsetName">对封装的表单、获取的网页内容进行的编码设置</param> /// <returns>Stream</returns> public Stream ConnectStream(String charsetName) { try { DateTime dt = new DateTime(); this.http.CookieContainer = new CookieContainer(); if (this.cookies.Count > 0) { String _c = HttpCommon.parse(HttpCommon.GetHttpCookies(this.cookies, Https), "; "); this.http.CookieContainer.SetCookies(http.RequestUri, _c); } byte[] arr = null; if (this.form.Count > 0) { String data = HttpCommon.format(form, charsetName); Encoding enc = Encoding.GetEncoding(28591); //28591对应iso-8859-1 arr = enc.GetBytes(data); } else if (this.databody != null) { arr = this.databody; } if (arr != null) { if (this.http.Method.ToUpper() == "GET") { this.http.Method = "POST"; } using (Stream stream = this.http.GetRequestStream()) { stream.Write(arr, 0, arr.Length); } } HttpWebResponse res = http.GetResponse() as HttpWebResponse; if (res.StatusCode == HttpStatusCode.OK) { List <Cookie> list = HttpCommon.GetHttpCookies(res.Cookies); foreach (Cookie m in list) { if (m.ExpiryDate == null) { this.AddCookie(m.Name, m.Value); // 会话cookie } else { if (!m.IsExpired(dt)) { this.AddCookie(m.Name, m.Value); } } } Stream stream = res.GetResponseStream(); return(stream); } } catch (Exception ex) { Console.WriteLine(ex.Message); } return(null); }
/// <summary> /// 连接并返回网页文本 /// </summary> /// <param name="charsetName">对封装的表单、获取的网页内容进行的编码设置</param> /// <returns>String</returns> public String Connect(String charsetName) { String result = null; try { DateTime dt = new DateTime(); this.http.CookieContainer = new CookieContainer(); if (this.cookies.Count > 0) { /* * List<Cookie> list = HttpCommon.GetHttpCookies(this.cookies, Https); * foreach (Cookie m in list) * { * if (m.ExpiryDate == null || (m.ExpiryDate != null && !m.IsExpired(dt))) * { * System.Net.Cookie x = new System.Net.Cookie(); * x.Name = m.Name; * x.Value = m.Value; * x.Secure = m.Secure; * x.Path = m.Path; * x.Domain = m.Domain; * x.Expires = m.ExpiryDate; * x.HttpOnly = m.HttpOnly; * this.http.CookieContainer.Add(x); * } * } */ String _c = HttpCommon.parse(HttpCommon.GetHttpCookies(this.cookies, Https), "; "); this.http.CookieContainer.SetCookies(http.RequestUri, _c); } byte[] arr = null; if (this.form.Count > 0) { String data = HttpCommon.format(form, charsetName); Encoding enc = Encoding.GetEncoding(28591); //28591对应iso-8859-1 arr = enc.GetBytes(data); } else if (this.databody != null) { arr = this.databody; } if (arr != null) { if (this.http.Method.ToUpper() == "GET") { this.http.Method = "POST"; } using (Stream stream = this.http.GetRequestStream()) { stream.Write(arr, 0, arr.Length); } } HttpWebResponse res = http.GetResponse() as HttpWebResponse; if (res.StatusCode == HttpStatusCode.OK) { List <Cookie> list = HttpCommon.GetHttpCookies(res.Cookies); foreach (Cookie m in list) { if (m.ExpiryDate == null) { this.AddCookie(m.Name, m.Value); // 会话cookie } else { if (!m.IsExpired(dt)) { this.AddCookie(m.Name, m.Value); } } } Stream stream = res.GetResponseStream(); Encoding ee = charsetName.ToLower().Equals("utf-8") ? new UTF8Encoding(false) : Encoding.GetEncoding(charsetName); StreamReader streamReader = new StreamReader(stream, ee); result = streamReader.ReadToEnd(); streamReader.Close(); stream.Close(); res.Close(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } return(result); }