获取指定页面的HTML源码

/// <summary> /// 获取指定页面的HTML源码 /// </summary> /// <param name="PageURL">目标网页地址</param> /// <param name="Encode">编码方式</param> /// <returns>目标网页源文件的HTML字符串</returns> public string GetPageCode(string PageURL, Encoding Encode) { //存放目标网页的html string strHtml = string.Empty; HttpWebResponse wresp; try { //连接到目标网页 WebRequest wreq = WebRequest.Create(PageURL); wresp = (HttpWebResponse)wreq.GetResponse(); } catch (WebException ex) { wresp = (HttpWebResponse)ex.Response; } //采用流读取,并确定编码方式 StreamReader objReader = new StreamReader(wresp.GetResponseStream(), Encode); return strHtml = objReader.ReadToEnd(); }

调用:

string PageCode = GetPageCode(url, Encoding.GetEncoding("gb2312")); 

 

 

于2010-09-06补上net_lover【孟子E章】方法:

//获取指定页面的源代码 public String GetPageCode(String PageURL, String Charset) { String strHtml = ""; try { //连接到目标网页 HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(PageURL); wreq.Method = "GET"; wreq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8"; HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse(); //采用流读取,并确定编码方式 Stream s = wresp.GetResponseStream(); StreamReader objReader = new StreamReader(s, System.Text.Encoding.GetEncoding(Charset)); strHtml = objReader.ReadToEnd(); objReader.Close(); return strHtml; } catch(WebException ex) { HttpWebResponse res = ex.Response as HttpWebResponse; if (res.StatusCode == HttpStatusCode.InternalServerError) { Stream s = res.GetResponseStream(); StreamReader objReader = new StreamReader(s, System.Text.Encoding.GetEncoding(Charset)); strHtml = objReader.ReadToEnd(); objReader.Close(); } else { strHtml = ex.Message; } return strHtml; } }

 

原帖地址:http://topic.csdn.net/u/20100904/09/0277af79-8e7a-43ca-ae25-538f5ac46b35.html

你可能感兴趣的:(html,windows,String,Stream,encoding,2010)