public void ReadCallback(IAsyncResult asyc) { string content = ""; string s = "utf-8"; string st = ""; HttpWebRequest request = (HttpWebRequest)asyc.AsyncState; HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyc); WebHeaderCollection HeadersStr; HeadersStr = request.Headers; Stream stream = response.GetResponseStream(); if (HeadersStr.HasKeys()) { string enstr = HeadersStr["Content-Type"]; if (enstr.IndexOf("charset") > 0) { st = s = enstr.Split('=')[1]; } } using (StreamReader rdsr = new StreamReader(stream, Encoding.GetEncoding(s))) { content = rdsr.ReadToEnd(); rdsr.Close(); } }
//默认根据utf-8编码 /// <summary> /// 通过网址获取页面内容 /// </summary> /// <returns></returns> public string getHtmlContentByUrl(bool _shieldIP = false) { string content = ""; string s = "utf-8"; string st = ""; WebHeaderCollection HeadersStr; WebClient webclient = new WebClient(); webclient.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1"; webclient.Headers["Accept-Charset"] = "GBK,utf-8;q=0.7,*;q=0.3"; try { Stream stream = webclient.OpenRead(this.Url); HeadersStr = webclient.ResponseHeaders; if (HeadersStr.HasKeys()) { string enstr = HeadersStr["Content-Type"]; if (enstr.IndexOf("charset") > 0) { st = s = enstr.Split('=')[1]; } } using (StreamReader rdsr = new StreamReader(stream, Encoding.GetEncoding(s))) { content = rdsr.ReadToEnd(); rdsr.Close(); } if (st.Length <= 0) { s = Html_Regex.RegexByStr(Html_Regex.Encodstr, content); if (s != "Error") { s = s.Replace("charset=", "").Trim('"').ToLower(); if (s != "utf-8" && !string.IsNullOrWhiteSpace(s)) { content = getHtmlContentByUrl(s); } } else { content = getHtmlContentByUrl("utf-8"); } } } catch (Exception e) { } return(content); }