-
C#教程之C#几种获取网页源文件代码的实例
方法一:
using System.Text;
using System.Net;
private string getHtml(string url)
{
WebClient myWebClient = new WebClient();
byte[] myDataBuffer = myWebClient.DownloadData (url);
return Encoding.Default.GetString(myDataBuffer);
}
方法二:
public string getHttp(string HttpUrl,string RefererUrl)
{
string html = "";
try
{
MSXML2.XMLHTTP Http = new MSXML2.XMLHTTPClass();
xmlhttp.open("GET", Url, false, null, null);
Http.open("GET",HttpUrl,false,null,null);
Http.setRequestHeader("Referer",RefererUrl);
//Http.setRequestHeader("Referer",RefererUrl);
Http.setRequestHeader("Content-Type", "text/html;charset=gb2312");
Http.send("");
html = Encoding.Default.GetString((byte[])Http.responseBody);
Http = null;
}
catch
{
}
return html;
}
public bool getweb(string strURL,out string buf)
{
buf="";
try
{
//Uri url=new Uri(strURL,false);
HttpWebRequest request;
request = (HttpWebRequest)WebRequest.Create(strURL);
request.Method="POST"; //Post请求方式
request.ContentType="text/html;charset=gb2312"; //内容类型
string paraUrlCoded = System.Web.HttpUtility.UrlEncode(""); //参数经过URL编码
byte[] payload;
payload = System.Text.Encoding.GetEncoding("GB2312").GetBytes(paraUrlCoded); //将URL编码后的字符串转化为字节
request.ContentLength = payload.Length; //设置请求的ContentLength
Stream writer = request.GetRequestStream(); //获得请求流
writer.Write(payload,0,payload.Length); //将请求参数写入流
writer.Close(); //关闭请求流
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse(); //获得响应流
Stream s;
s = response.GetResponseStream();
StreamReader objReader = new StreamReader(s,System.Text.Encoding.GetEncoding("GB2312"));
string HTML = "";
string sLine ="";
int i = 0;
while (sLine!=null)
{
i++;
sLine = objReader.ReadLine();
if (sLine!=null)
HTML += sLine;
}
//HTML = HTML.Replace("<","<");
//HTML = HTML.Replace(">",">");
buf=HTML;
return true;
}
catch (Exception x)
{
buf=x.Message.ToString();
return false;
}
}
带Cookie:
CookieContainer cc = new CookieContainer();
public bool getweb(string strURL,out string buf)
{
buf="";
try
{
HttpWebRequest request;
request = (HttpWebRequest)WebRequest.Create(strURL);
request.Method="POST"; //Post请求方式
request.ContentType="text/html;charset=gb2312"; //内容类型
string paraUrlCoded = System.Web.HttpUtility.UrlEncode(""); //参数经过URL编码
byte[] payload;
payload = System.Text.Encoding.GetEncoding("GB2312").GetBytes(paraUrlCoded); //将URL编码后的字符串转化为字节
request.ContentLength = payload.Length; //设置请求的ContentLength
Stream writer = request.GetRequestStream(); //获得请求流
writer.Write(payload,0,payload.Length); //将请求参数写入流
writer.Close(); //关闭请求流
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse(); //获得响应流
Stream s;
s = response.GetResponseStream();
StreamReader objReader = new StreamReader(s,System.Text.Encoding.GetEncoding("GB2312"));
string HTML = "";
string sLine ="";
int i = 0;
while (sLine!=null)
{
i++;
sLine = objReader.ReadLine();
if (sLine!=null)
HTML += sLine;
}
buf=HTML;
return true;
}
catch (Exception x)
{
buf=x.Message.ToString();
return false;
}
}
public bool getweb(string strURL,out string buf,string postData)
{
buf="";
try
{
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
request.CookieContainer = cc;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cc.Add(response.Cookies);
Stream stream = response.GetResponseStream();
string sHtml = new StreamReader(stream, System.Text.Encoding.Default).ReadToEnd();
buf=sHtml;
return true;
}
catch (Exception x)
{
buf=x.Message.ToString();
return false;
}
}
private string getWebresourceFile1(string url)
{
WebClient myWebClient = new WebClient();
byte[] myDataBuffer = myWebClient.DownloadData(url);
string SourceCode = Encoding.Default.GetString(myDataBuffer);
saveSourceCode(SourceCode);
return SourceCode;
}
方法2
private string getWebresourceFile2(string url)
{
HttpWebRequest request=(HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response=(HttpWebResponse)request.GetResponse();
request.Method = "GET ";
Stream receiveStream=response.GetResponseStream();
StreamReader readStream=new StreamReader(receiveStream,Encoding.Default);
string SourceCode=readStream.ReadToEnd();
saveSourceCode(SourceCode);
response.Close();
readStream.Close();
return SourceCode;
}
方法3
private string getWebresourceFile3(string url)
{
WebClient wc = new WebClient();
wc.Credentials = CredentialCache.DefaultCredentials;
Byte[] pageData = wc.DownloadData(url);
string SourceCode = Encoding.Default.GetString(pageData);
saveSourceCode(SourceCode);
wc.Dispose();
return SourceCode;
}
方法4
private string getWebresourceFile4(string url)
{
WebClient wc = new WebClient();
wc.Credentials = CredentialCache.DefaultCredentials;
Stream resStream = wc.OpenRead(url);
StreamReader sr = new StreamReader(resStream,System.Text.Encoding.Default);
string SourceCode = sr.ReadToEnd();
saveSourceCode(SourceCode);
resStream.Close();
wc.Dispose();
return SourceCode;
}
方法5
private string getWebresourceFile5(string url)
{
WebRequest request = WebRequest.Create(url);
WebResponse response = request.GetResponse();
Stream resStream = response.GetResponseStream();
StreamReader sr = new StreamReader(resStream, System.Text.Encoding.Default);
string SourceCode = sr.ReadToEnd();
saveSourceCode(SourceCode);
resStream.Close();
sr.Close();
return SourceCode;
}
复制代码 代码如下:
using System.Text;
using System.Net;
private string getHtml(string url)
{
WebClient myWebClient = new WebClient();
byte[] myDataBuffer = myWebClient.DownloadData (url);
return Encoding.Default.GetString(myDataBuffer);
}
方法二:
复制代码 代码如下:
public string getHttp(string HttpUrl,string RefererUrl)
{
string html = "";
try
{
MSXML2.XMLHTTP Http = new MSXML2.XMLHTTPClass();
xmlhttp.open("GET", Url, false, null, null);
Http.open("GET",HttpUrl,false,null,null);
Http.setRequestHeader("Referer",RefererUrl);
//Http.setRequestHeader("Referer",RefererUrl);
Http.setRequestHeader("Content-Type", "text/html;charset=gb2312");
Http.send("");
html = Encoding.Default.GetString((byte[])Http.responseBody);
Http = null;
}
catch
{
}
return html;
}
public bool getweb(string strURL,out string buf)
{
buf="";
try
{
//Uri url=new Uri(strURL,false);
HttpWebRequest request;
request = (HttpWebRequest)WebRequest.Create(strURL);
request.Method="POST"; //Post请求方式
request.ContentType="text/html;charset=gb2312"; //内容类型
string paraUrlCoded = System.Web.HttpUtility.UrlEncode(""); //参数经过URL编码
byte[] payload;
payload = System.Text.Encoding.GetEncoding("GB2312").GetBytes(paraUrlCoded); //将URL编码后的字符串转化为字节
request.ContentLength = payload.Length; //设置请求的ContentLength
Stream writer = request.GetRequestStream(); //获得请求流
writer.Write(payload,0,payload.Length); //将请求参数写入流
writer.Close(); //关闭请求流
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse(); //获得响应流
Stream s;
s = response.GetResponseStream();
StreamReader objReader = new StreamReader(s,System.Text.Encoding.GetEncoding("GB2312"));
string HTML = "";
string sLine ="";
int i = 0;
while (sLine!=null)
{
i++;
sLine = objReader.ReadLine();
if (sLine!=null)
HTML += sLine;
}
//HTML = HTML.Replace("<","<");
//HTML = HTML.Replace(">",">");
buf=HTML;
return true;
}
catch (Exception x)
{
buf=x.Message.ToString();
return false;
}
}
带Cookie:
复制代码 代码如下:
CookieContainer cc = new CookieContainer();
public bool getweb(string strURL,out string buf)
{
buf="";
try
{
HttpWebRequest request;
request = (HttpWebRequest)WebRequest.Create(strURL);
request.Method="POST"; //Post请求方式
request.ContentType="text/html;charset=gb2312"; //内容类型
string paraUrlCoded = System.Web.HttpUtility.UrlEncode(""); //参数经过URL编码
byte[] payload;
payload = System.Text.Encoding.GetEncoding("GB2312").GetBytes(paraUrlCoded); //将URL编码后的字符串转化为字节
request.ContentLength = payload.Length; //设置请求的ContentLength
Stream writer = request.GetRequestStream(); //获得请求流
writer.Write(payload,0,payload.Length); //将请求参数写入流
writer.Close(); //关闭请求流
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse(); //获得响应流
Stream s;
s = response.GetResponseStream();
StreamReader objReader = new StreamReader(s,System.Text.Encoding.GetEncoding("GB2312"));
string HTML = "";
string sLine ="";
int i = 0;
while (sLine!=null)
{
i++;
sLine = objReader.ReadLine();
if (sLine!=null)
HTML += sLine;
}
buf=HTML;
return true;
}
catch (Exception x)
{
buf=x.Message.ToString();
return false;
}
}
public bool getweb(string strURL,out string buf,string postData)
{
buf="";
try
{
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
request.CookieContainer = cc;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cc.Add(response.Cookies);
Stream stream = response.GetResponseStream();
string sHtml = new StreamReader(stream, System.Text.Encoding.Default).ReadToEnd();
buf=sHtml;
return true;
}
catch (Exception x)
{
buf=x.Message.ToString();
return false;
}
}
private string getWebresourceFile1(string url)
{
WebClient myWebClient = new WebClient();
byte[] myDataBuffer = myWebClient.DownloadData(url);
string SourceCode = Encoding.Default.GetString(myDataBuffer);
saveSourceCode(SourceCode);
return SourceCode;
}
方法2
复制代码 代码如下:
private string getWebresourceFile2(string url)
{
HttpWebRequest request=(HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response=(HttpWebResponse)request.GetResponse();
request.Method = "GET ";
Stream receiveStream=response.GetResponseStream();
StreamReader readStream=new StreamReader(receiveStream,Encoding.Default);
string SourceCode=readStream.ReadToEnd();
saveSourceCode(SourceCode);
response.Close();
readStream.Close();
return SourceCode;
}
方法3
复制代码 代码如下:
private string getWebresourceFile3(string url)
{
WebClient wc = new WebClient();
wc.Credentials = CredentialCache.DefaultCredentials;
Byte[] pageData = wc.DownloadData(url);
string SourceCode = Encoding.Default.GetString(pageData);
saveSourceCode(SourceCode);
wc.Dispose();
return SourceCode;
}
方法4
复制代码 代码如下:
private string getWebresourceFile4(string url)
{
WebClient wc = new WebClient();
wc.Credentials = CredentialCache.DefaultCredentials;
Stream resStream = wc.OpenRead(url);
StreamReader sr = new StreamReader(resStream,System.Text.Encoding.Default);
string SourceCode = sr.ReadToEnd();
saveSourceCode(SourceCode);
resStream.Close();
wc.Dispose();
return SourceCode;
}
方法5
复制代码 代码如下:
private string getWebresourceFile5(string url)
{
WebRequest request = WebRequest.Create(url);
WebResponse response = request.GetResponse();
Stream resStream = response.GetResponseStream();
StreamReader sr = new StreamReader(resStream, System.Text.Encoding.Default);
string SourceCode = sr.ReadToEnd();
saveSourceCode(SourceCode);
resStream.Close();
sr.Close();
return SourceCode;
}
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式