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