代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
获取网页源代码
作者:
dezai
/ 发布于
2014/9/16
/
1035
方法二: 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; }
试试其它关键字
网页源代码
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
dezai
贡献的其它代码
(
1065
)
.
双色球
.
列出所有物理网络适配器
.
快乐数的 Python 实现
.
计算当月还剩天数
.
猜属相
.
二十四小时时钟
.
每日一语
.
很酷的日历
.
超长日历表单
.
最简单的时钟
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3