- 爱易网页
-
ASP.NET教程
- 模拟浏览器浏览网页的有关问题
日期:2014-05-17 浏览次数:20446 次
模拟浏览器浏览网页的问题
在网上看到了这个方法,
public String GetPageCode(String PageURL, String Charset)
{
try
{
String strHtml = "";
HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(PageURL);
CookieContainer cookieCon = new CookieContainer();
wreq.CookieContainer = cookieCon;
HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse();
Stream s = wresp.GetResponseStream();
StreamReader objReader = new StreamReader(s, System.Text.Encoding.GetEncoding(Charset));
string strLine = "";
while (strLine != null)
{
strLine = objReader.ReadLine();
if (strLine != null)
{
strHtml += strLine.Trim();
}
}
strHtml = strHtml.Replace("<br />", "\r\n");
return strHtml;
}
catch (Exception n) //遇到错误,打印错误
{
return n.Message;
}
}
然后就执行了下,但是都是得到乱码,能帮忙看下吗?(换了其他的编码也是一样)
protected void Button2_Click(object sender, EventArgs e)
{
string sourcecode = GetPageCode("http://www.google.com.hk/search?q=NBA&hl=zh-CN&newwindow=1&safe=strict&prmd=ivns&ei=3cBGUJW-O--fiAeUqoHYAg&start=20&sa=N", "utf-8");
TextBox2.Text = sourcecode;
}