Dub编程网 - 普及全民编程,为分享网络资源而生
Dub编程网以编程学习为主,站内涵盖了C#、C++、易语言等一系列教程和学习资料!

通过http协议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 �0�2 string �0�2 getWebresourceFile1(string �0�2 url)

{

 

WebClient �0�2 myWebClient �0�2 = �0�2 new �0�2 WebClient();

byte[] �0�2 myDataBuffer �0�2 = �0�2 myWebClient.DownloadData(url);

string �0�2 SourceCode �0�2 = �0�2 Encoding.Default.GetString(myDataBuffer);

saveSourceCode(SourceCode);

return �0�2 SourceCode;

}

 

方法2

private �0�2 string �0�2 getWebresourceFile2(string �0�2 url)

{

HttpWebRequest �0�2 request=(HttpWebRequest)WebRequest.Create(url);

HttpWebResponse �0�2 response=(HttpWebResponse)request.GetResponse();

request.Method �0�2 = �0�2 "GET ";

Stream �0�2 receiveStream=response.GetResponseStream();

StreamReader �0�2 readStream=new �0�2 StreamReader(receiveStream,Encoding.Default);

string �0�2 SourceCode=readStream.ReadToEnd();

saveSourceCode(SourceCode);

response.Close();

readStream.Close();

return �0�2 SourceCode;

 

}

方法3

private �0�2 string �0�2 getWebresourceFile3(string �0�2 url)

{

WebClient �0�2 wc �0�2 = �0�2 new �0�2 WebClient();

wc.Credentials �0�2 = �0�2 CredentialCache.DefaultCredentials;

Byte[] �0�2 pageData �0�2 = �0�2 wc.DownloadData(url);

string �0�2 SourceCode �0�2 = �0�2 Encoding.Default.GetString(pageData);

saveSourceCode(SourceCode);

wc.Dispose();

return �0�2 SourceCode;

}

 

方法4

private �0�2 string �0�2 getWebresourceFile4(string �0�2 url)

{

WebClient �0�2 wc �0�2 = �0�2 new �0�2 WebClient();

wc.Credentials �0�2 = �0�2 CredentialCache.DefaultCredentials;

Stream �0�2 resStream �0�2 = �0�2 wc.OpenRead(url);

StreamReader �0�2 sr �0�2 = �0�2 new �0�2 StreamReader(resStream,System.Text.Encoding.Default);

 

string �0�2 SourceCode �0�2 = �0�2 sr.ReadToEnd();

saveSourceCode(SourceCode);

resStream.Close();

wc.Dispose();

return �0�2 SourceCode;

}

方法5

private �0�2 string �0�2 getWebresourceFile5(string �0�2 url)

{

WebRequest �0�2 request �0�2 = �0�2 WebRequest.Create(url);

WebResponse �0�2 response �0�2 = �0�2 request.GetResponse();

Stream �0�2 resStream �0�2 = �0�2 response.GetResponseStream();

StreamReader �0�2 sr �0�2 = �0�2 new �0�2 StreamReader(resStream, �0�2 System.Text.Encoding.Default);

string �0�2 SourceCode �0�2 = �0�2 �0�2 sr.ReadToEnd();

saveSourceCode(SourceCode);

resStream.Close();

sr.Close();

return �0�2 SourceCode;

}


标签:
分类:编程技术| 发布:admin| 查看: | 发表时间:2013/7/28
原创文章如转载,请注明:转载自Dub编程网 http://www.pcdub.com/
本文链接:http://www.pcdub.com/post/8.html

已经有 ( 1 ) 位网友发表了评论,你也评一评吧!