200分请问如何下载HTML代码 因为各页面编码不同,有的WEB服务器发送编码格式,有的没有发送,导致我下在下来的byte[]转化成string时出错,求能真正转化的代码。很多代码并不能对各种页面下载。(我已经测试过了)解决者另开贴送100分。谢谢。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 #region 取得网页源代码 /// <summary> /// 取得网页源代码 /// </summary> /// <param name="strUrl">要取的URL</param> /// <returns>网页源代码</returns> public string GetWebCode(string strUrl) { System.Net.HttpWebRequest httpReq; System.Net.HttpWebResponse httpResp; System.Uri httpURL = new System.Uri(strUrl); httpReq = ((HttpWebRequest)(WebRequest.Create(httpURL))); httpReq.Method = "GET"; httpResp = ((HttpWebResponse)(httpReq.GetResponse())); httpReq.KeepAlive = false; StreamReader reader = new StreamReader(httpResp.GetResponseStream(), System.Text.Encoding.GetEncoding("GB2312")); string respHTML = reader.ReadToEnd(); return respHTML ; } #endregion 有人说http头里面肯定包含编码信息的分析就可以了我没测试过:)不过最笨的办法可以把下载的HTML分析字符串里面肯定有编码信息的 有的网站头里包含编码信息,有的不包含下载下来HTML,根本解析不出来,怎么找编码? 怎么会解析不出来???用unicode编码大不了文字乱码英文怎么会是乱码??? 你运行一下试试:using System;using System.Threading;using System.Net;using System.IO;public class WorkerThreadExample{ static void Main(string[] args) { // Create the thread object. This does not start the thread. Console.WriteLine(args[0]); string html = GetWebCode(args[0]); Console.WriteLine(html); } #region 取得网页源代码 /// <summary> /// 取得网页源代码 /// </summary> /// <param name="strUrl">要取的URL</param> /// <returns>网页源代码</returns> public static string GetWebCode(string strUrl) { System.Net.HttpWebRequest httpReq; System.Net.HttpWebResponse httpResp; System.Uri httpURL = new System.Uri(strUrl); httpReq = ((HttpWebRequest)(WebRequest.Create(httpURL))); httpResp = ((HttpWebResponse)(httpReq.GetResponse())); httpReq.KeepAlive = false; StreamReader reader = new StreamReader(httpResp.GetResponseStream(), System.Text.Encoding.GetEncoding("unicode")); return reader.ReadToEnd(); } #endregion} WebClient和System.Text.....具体自己查一下 读stream可以用 SreamReader(stream,encoding.utf8)来ReadToEnd 成 string 尽管可能是乱码,但是英文是没乱的然后查找 charset 的属性关键是这样这个stream就用掉了,要重新抓一个当然 如果是utf-8就抓一次就好了我用的是utf-8解码,看来需要统计一下哪种编码比较多... 确认一下 WebResponse.Headers 里面带编码的网站很少。解决这个问题只能向IE浏览器学习用meta里面的 charset来确定编码 Sorry我说错了应该是UTF8你可以试试public static string GetWebCode(string strUrl){ string s = Encoding.UTF8.GetString(new WebClient().DownloadData(strUrl)); Console.WriteLine(s); return s;} 谢谢楼上,这个办法还可以,不过如果页面里没有charset怎么办? 在meta中没有charset的页面基本上很少吧??可以说没有呵呵你肯定要try的啊如果没有这个页面就忽略掉咯~~~~~~~ 找到了charset,然后呢?把string再转成byte[],然后再根据得到的编码重新转是吗?能大概给个代码或者伪代码吗?谢谢 你说的就对了呵呵你在得到远程数据的时候把byte[]保存下来这样你只要根据编码重新得到一下string就可以了没必要重新连接public static string GetWebCode(string strUrl){ byte[] b = new WebClient().DownloadData(strUrl); string charset = FindCharset(Encoding.UTF8.GetString(b)); return Encoding.GetEncoding(charset).GetString(b);}private string FindCharset(string str){ string charset = ""; //找charset return charset;} System.Windows.Forms.WebBrowser webBrowser = new WebBrowser();webBrowser.Url = new uri("http;//www.baidu.com");when webbrowser -> DocumentCompletedthen text:string strHtml = webbrowser.DocumentText;type:webbrowser.DocumentType;up 而且一个网站的页面一般只会是一种编码所以你可以弄个hashtable之类的保存这个网站的编码一个网站只找一次charset WebClient wc = new WebClient();string content = wc.DownloadString(url); 谢谢 lovefootball(蟑螂(生活就是扯淡--做人要放低姿态)) 我先试试,能加你QQ或者MSN吗?我的QQ:4111852MSN:iuhxq#hotmail.com 你还没解决呢?去看看 蒋晟 写的代码吧http://community.csdn.net/Expert/topic/5169/5169854.xml?temp=.2164881 部署安装,更改原程序名 怎么定时读时网页呀 WinForm怎么计算控件在窗口中的位置 datagrid(c#)如何绑定数据库 【求助】用c#将的Excel数据保存到ACCESS中 如果仅仅是对数据进行增删改查的基本操作还用使用业务层吗? xpath取节点问题 linq where 00开头的字符串 定时器的启动问题(比较怪异,这种方式就是触发不了) DataAdapter 更新数据时候出错 WinForm中的数据库连接 treeView 和 ContextMenu 的问题
#region 取得网页源代码
/// <summary>
/// 取得网页源代码
/// </summary>
/// <param name="strUrl">要取的URL</param>
/// <returns>网页源代码</returns>
public string GetWebCode(string strUrl)
{
System.Net.HttpWebRequest httpReq;
System.Net.HttpWebResponse httpResp;
System.Uri httpURL = new System.Uri(strUrl);
httpReq = ((HttpWebRequest)(WebRequest.Create(httpURL)));
httpReq.Method = "GET";
httpResp = ((HttpWebResponse)(httpReq.GetResponse()));
httpReq.KeepAlive = false;
StreamReader reader = new StreamReader(httpResp.GetResponseStream(), System.Text.Encoding.GetEncoding("GB2312"));
string respHTML = reader.ReadToEnd();
return respHTML ;
}
#endregion
分析就可以了
我没测试过:)不过最笨的办法
可以把下载的HTML分析字符串
里面肯定有编码信息的
用unicode编码
大不了文字乱码
英文怎么会是乱码???
using System.Threading;
using System.Net;
using System.IO;public class WorkerThreadExample
{
static void Main(string[] args)
{
// Create the thread object. This does not start the thread.
Console.WriteLine(args[0]);
string html = GetWebCode(args[0]);
Console.WriteLine(html);
} #region 取得网页源代码
/// <summary>
/// 取得网页源代码
/// </summary>
/// <param name="strUrl">要取的URL</param>
/// <returns>网页源代码</returns>
public static string GetWebCode(string strUrl)
{
System.Net.HttpWebRequest httpReq;
System.Net.HttpWebResponse httpResp;
System.Uri httpURL = new System.Uri(strUrl);
httpReq = ((HttpWebRequest)(WebRequest.Create(httpURL)));
httpResp = ((HttpWebResponse)(httpReq.GetResponse()));
httpReq.KeepAlive = false;
StreamReader reader = new StreamReader(httpResp.GetResponseStream(), System.Text.Encoding.GetEncoding("unicode"));
return reader.ReadToEnd();
}
#endregion
}
尽管可能是乱码,但是英文是没乱的
然后查找 charset 的属性关键是这样这个stream就用掉了,要重新抓一个
当然 如果是utf-8就抓一次就好了我用的是utf-8解码,看来需要统计一下哪种编码比较多...
用meta里面的 charset来确定编码
我说错了
应该是UTF8你可以试试
public static string GetWebCode(string strUrl)
{
string s = Encoding.UTF8.GetString(new WebClient().DownloadData(strUrl));
Console.WriteLine(s);
return s;
}
可以说没有
呵呵你肯定要try的啊
如果没有这个页面就忽略掉咯~~~~~~~
呵呵你在得到远程数据的时候
把byte[]保存下来
这样你只要根据编码重新得到一下string就可以了
没必要重新连接public static string GetWebCode(string strUrl)
{
byte[] b = new WebClient().DownloadData(strUrl);
string charset = FindCharset(Encoding.UTF8.GetString(b));
return Encoding.GetEncoding(charset).GetString(b);
}
private string FindCharset(string str)
{
string charset = "";
//找charset
return charset;
}
then text:
string strHtml = webbrowser.DocumentText;type:
webbrowser.DocumentType;up
所以你可以弄个hashtable之类的保存这个网站的编码
一个网站只找一次charset
string content = wc.DownloadString(url);
去看看 蒋晟 写的代码吧http://community.csdn.net/Expert/topic/5169/5169854.xml?temp=.2164881