C# HttpWebRequest 请求网页时候,怎么判断网页编码

作者&投稿:镇劳 (若有异议请与网页底部的电邮联系)
C#使用(HttpWebRequest)WebRequest.Create()判断是否连接服务器时,请求时间过久~

改方法有设置超时时间的。
因为默认的超时时间很长。
所以不报错。

网站打开的速度与你程序大小,网络快慢,资料文件大小都有关,如果你只是想看看你的网站别人打开的速度,你可以用上面的网址测试一下:
http://www.webwait.com/
http://www.webslug.info/
http://tools.pingdom.com/

先判断Response的Content-Type中的charset数据。如果不行,再判断http的meta中的content-type中的charset数据。再不行,只能测试是不是某些主流编码了(例如:UTF-8,GB2312,GBK之类的)。我这里的代码没有测试主流编码,只实现了前面的判断。
static void Main(string[] args)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(/*要获取源码的网站*/);
HttpWebResponse reponse = (HttpWebResponse)request.GetResponse();
string contentType = reponse.Headers["Content-Type"];
Encoding encoding = null;
Regex regex = new Regex("charset\\s*=\\s*(\\S+)", RegexOptions.IgnoreCase);
Match match = null;
if (contentType != null)
{
match = regex.Match(contentType);
if (match.Success)
{
try
{
encoding = Encoding.GetEncoding(match.Groups[1].Value.Trim());
using (TextReader reader = new StreamReader(reponse.GetResponseStream(), encoding))
{
string str = reader.ReadToEnd();
Console.WriteLine(str);
}
}
catch (Exception exx)
{
Console.WriteLine(exx);
}
}
}
if (contentType == null || (!match.Success))
{
using (TextReader reader = new StreamReader(reponse.GetResponseStream(), Encoding.Default))
{
string str = reader.ReadToEnd();
regex = new Regex("<\\s*meta.+charset\\s*=\\s*(\\S+)\\s*\"", RegexOptions.IgnoreCase);
match = regex.Match(str);
if (match.Success)
{
try
{
encoding = Encoding.GetEncoding(match.Groups[1].Value.Trim());
str = encoding.GetString(Encoding.Default.GetBytes(str));
Console.WriteLine(str);
}
catch (Exception exx)
{
Console.WriteLine(exx);
}
}
}
}

Console.ReadKey();
}

响应头啊,里面不是有contenttype么


盈江县15694131287: c# httpwebrequest循环请求网页 -
肥崔彼赛: 两种方法都可以~~ 利于 HttpWebRquest 返回的是字符串~~ 里面是该网页的源码~~ 要想到的它里面的超级链接不难~ 可以利于正则表达式分析并读取~~ 当然,你也可以自己判断~~ 而使用 WebBrowser 可能更直观一些~~ 这时候需要进行 DOM 操作了~~ 究竟使用哪种方法,就看你更熟悉哪个了..而至于要实现循环请求~~~ 则可以使用递归技术~~

盈江县15694131287: C#如何利用HttpWebRequest来获取url的页面内容 -
肥崔彼赛: WebRequest request = WebRequest.Create("http://www.baidu.com");//为指定的 URI 方案初始化新的 System.Net.WebRequest 实例 request.UseDefaultCredentials = false;//获取或设置一个 System.Boolean 值,该值控制 System.Net....

盈江县15694131287: C# 如何用HttpWebReques获得HTTP响应的数据包??? -
肥崔彼赛: HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URL); req.Timeout = 15000; //超时时间 req.CookieContainer = new CookieContainer(); req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; ....

盈江县15694131287: C#,WebRequest类和HttpWebRequest类的区别?
肥崔彼赛: httpWebRequest是webRequest的子类,httpWebRequest是基于http协议的 . HttpWebRequest 是 WebRequest 的实例化使用,单独的 WebRequest 是不能使用的 在使用HttpWebRequest实例,我们不用使用HttpWebRequest类的构造函数,而是使用WebRequest类提供的静态方法,然后强制转换,如: HttpWebResponse webResponse = (HttpWebResponse) webRequest.GetResponse();

盈江县15694131287: C# HttpWebRequest怎样获取到HttpOnly的Cookie -
肥崔彼赛: 方法如下:byte[] bytes = Encoding.Default.GetBytes(_post); CookieContainer myCookieContainer = new CookieContainer(); try { //新建一个CookieContainer HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(_loginurl...

盈江县15694131287: c# .net 怎么判断 WebRequest 请求失败了 还是 成功了? -
肥崔彼赛: HttpWebRequest请求超时异常的处理方法(重新发送请求到成功为止)方法主要有2点:1.在GetResponse()超时后要重新发送请求;2.检查是否异常,捕获或抛掉异常.publicstring GetHtml(string URI) {string fullhtml=null;while(true) {...

盈江县15694131287: 如何在c#中使用webRequest 获取网页中的内容 在线等 -
肥崔彼赛: 首先是获取网页内容的问题,用webRequest,然后用输入输出流就可以了,要是想获取某一部分特定的内容的话必须用到正则表达式,.net中有一个正则表达式的类库,楼主可以看看

盈江县15694131287: C# 怎么利用HttpWebRequest获取当前页面的cookies -
肥崔彼赛: 用这个类吧HttpCookie . HttpCookie c = new HttpCookie("userid", "admin"); HttpContext.Current.Response.Cookies.A

盈江县15694131287: C#HttpWebRequest使用 -
肥崔彼赛: private bool AutoLogon(string userId, string password) { try { #region 获取登录FORM的输入框 和 Submit 按钮 HtmlElement textboxUserId = this.webBrowser1.Document.GetElementById("username"); //如果没有ID,用 Name 获取 //...

盈江县15694131287: [C#]使用HttpWebRequest请求远端服务器时如何加载SSL证书 -
肥崔彼赛: url=/library/en-us/cpref/html/frlrfsystemsecuritycryptographyx509certificatesx509certificateclasstopic.asp.之后我们就可以///构建请求的HttpWebRequest对象HttpWebRequest hwrRequest = (HttpWebRequest)WebRequest.Create( ??????????????...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网