程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> C#幾種獲取網頁源文件代碼的實例

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;
      }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved