c#利用HttpWebRequest获取网页源代码
c#利用HttpWebRequest获取网页源代码,搞了好几天终于解决了,直接获取网站编码进行数据读取,再也不用担心乱码了!
命名空间:Using System.Net
private static string GetUrlHtml(string url)
{ string strHtml = string.Empty; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse();//从Internet资源返回数据流 if (response.CharacterSet.ToLower() == "gbk")
{
using (Stream respStream = response.GetResponseStream())//读取数据流
{
using (StreamReader str = new StreamReader(respStream, Encoding.GetEncoding("gb2312")))//读取数据
{
strHtml = str.ReadToEnd();
}
}
}
else
{
using (Stream respStream = response.GetResponseStream())//读取数据流
{
using (StreamReader str = new StreamReader(respStream, Encoding.UTF8))//读取数据
{
strHtml = str.ReadToEnd();
}
}
}
return strHtml;
}
c#利用HttpWebRequest获取网页源代码的更多相关文章
- 用asp.net c# HttpWebRequest获取网页源代码
public string GetPage(string url) { HttpWebRequest request = null; HttpWebResponse response = null; ...
- c#利用WebClient和WebRequest获取网页源代码的比较
前几天举例分析了用asp+xmlhttp获取网页源代码的方法,但c#中一般是可以利用WebClient类和WebRequest类获取网页源代码.下面分别说明这两种方法的实现. WebClient类获取 ...
- c#利用WebClient和WebRequest获取网页源代码
C#中一般是可以利用WebClient类和WebRequest类获取网页源代码.下面分别说明这两种方法的实现. WebClient类获取网页源代码 WebClient类 WebClient ...
- Java 网络爬虫获取网页源代码原理及实现
Java 网络爬虫获取网页源代码原理及实现 1.网络爬虫是一个自动提取网页的程序,它为搜索引擎从万维网上下载网页,是搜索引擎的重要组成.传统爬虫从一个或若干初始网页的URL开始,获得初始网页上的URL ...
- delphi 获取网页源代码
//获取网页源代码 var s: string; begin s := WebBrowser1.OleObject.document.body.innerHTML; //body内的所有代码 ...
- JS远程获取网页源代码的例子
js代码获取网页源代码. 代码: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> < ...
- js技术要点---JS 获取网页源代码
JS 获取网页源代码 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html& ...
- C# 获取网页源代码
/// <summary> /// 获取网页源代码 /// </summary> /// <param name="url"></para ...
- NodeJS 获取网页源代码
获取网页源代码 node 获取网页源代码 var http = require('http'); var url = "http://www.baidu.com/"; // 参数u ...
随机推荐
- 自律训练法 John Sehorz
自律训练法,系1932年由德国精神医学医师John Sehorz所创立.他研究人们在催眠催眠状态下,所呈现的生理状态,如:沉重与温暖感.. ,因而,John Sehorz改以「逆向操作」之方式,由自我 ...
- QQ在线客服
css代码: .float0831 { POSITION: fixed; TOP: 180px; RIGHT: 1px; _position: absolute } .float0831 A { CO ...
- jq改变DIV中图片的路径
<script src="common/jquery.js"></script> <script language="javascript ...
- Android高清巨图加载方案
1.今天看了鸿洋的<Android高清巨图加载方案>一文,对加载高清巨图时的解决方案有了一定的认识. 思路为: 提供一个设置图片的入口. 重写onTouchEvent,在里面根据用户移动的 ...
- pyqt5通过文本对话框打开文件
点击按钮,打开文本对话框,找一人文件,打开并显示内容 QFIleDialog ...
- C语言初学 判断闰年的问题
#include<stdio.h> main( ) { int year , leap; scanf("%d",&year); i ...
- C语言之链表————(转载)
#include <stdio.h>#include <malloc.h>#define LEN sizeof(struct student) /*-------------- ...
- 用js实现两个select下拉框之间的元素互相移动
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Ubuntu12.04 下svn服务搭建及Windows客户端tortoisesvn的使用
在Ubuntu服务端搭建apache+svn 在客户端使用Tortoisesvn工具. 第一步 安装SVN $sudo apt-get install subversion 安装成功后系统会自动建立一 ...
- TEncoding & TNetEncoding(使用现成的TBase64Encoding,TEncoding和TMBCSEncoding)
TEncoding and TNetEncoding are abstract classes and you will never instantiate one of them, because ...