asp.Net Webソースをキャプチャする3つの実現方法

5011 ワード

方法1比較推奨

///   
 
    ///  HttpWebRequest        
    ///    BOM      ,               
    ///   
    ///     "   
    ///          
    public static string GetHtmlSource2(string url)
    {
      //      
      string html = "";
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
      request.Accept = "*/*"; //      
      request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)"; //     IE    http://www.52mvc.com
      request.AllowAutoRedirect = true;//    302
      //request.CookieContainer = new CookieContainer();//cookie  ,
      request.Referer = url; //       
 
 
      HttpWebResponse response = (HttpWebResponse)request.GetResponse();
      Stream stream = response.GetResponseStream();
      StreamReader reader = new StreamReader(stream, Encoding.Default);
      html = reader.ReadToEnd();
      stream.Close();
 
 
      return html;
    }

方法2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
using System.Net;

namespace MySql
{
  public class GetHttpData
  {
    public static string GetHttpData2(string Url)
    {
      string sException = null;
      string sRslt = null;
      WebResponse oWebRps = null;
      WebRequest oWebRqst = WebRequest.Create(Url);
      oWebRqst.Timeout = 50000;
      try
      {

        oWebRps = oWebRqst.GetResponse();

      }
      catch (WebException e)
      {
        sException = e.Message.ToString();
      }
      catch (Exception e)
      {
        sException = e.ToString();
 
      }
      finally
      {
        if (oWebRps != null)
        {
 
          StreamReader oStreamRd = new StreamReader(oWebRps.GetResponseStream(), Encoding.GetEncoding("utf-8"));
          sRslt = oStreamRd.ReadToEnd();
          oStreamRd.Close();
          oWebRps.Close();
        }
      }
 
      return sRslt;
    }
 
  }
}

方法3

public static string getHtml(string url, params string [] charSets)//url         ,charSet        ,      null  "",           
  {
    try
    {
      string charSet = null;
      if (charSets.Length == 1) {
        charSet = charSets[0];
      }
      WebClient myWebClient = new WebClient(); //  WebClient  myWebClient
      //      :
      //          ,         cookie,      
      //                   cookie
      // webclient.Headers.Add("Cookie", cookie);
      //            。         
 
 
      //          Internet                 。
      myWebClient.Credentials = CredentialCache.DefaultCredentials;
      //           ,  
      //NetworkCredential mycred = new NetworkCredential(struser, strpassword);
      //myWebClient.Credentials = mycred;
      //              。( @        "/"  )
      byte[] myDataBuffer = myWebClient.DownloadData(url);
      string strWebData = Encoding.Default.GetString(myDataBuffer);
 
 
      //            
      Match charSetMatch = Regex.Match(strWebData, " 
 

asp.Net Webソースファイルの取得方法
Webソースファイルを取得する必要がある場合がありますので、以下の方法で簡単にタスクを完了できます.

private string GetStringByUrl(string strUrl) 
{ 
  WebRequest wrt = WebRequest.Create(strUrl); 
  WebResponse wrse = wrt.GetResponse(); 
  Stream strM = wrse.GetResponseStream(); 
  StreamReader SR = new StreamReader(strM,  Encoding.GetEncoding("gb2312")); 
  string strallstrm = SR.ReadToEnd(); 
  return strallstrm; 
} 

ダウンロードするページのアドレスさえ伝わればOK!この方法でソースコードをエクスポートします.

private string SaveHTML() 
 {     
string str = RenderPage("Default2.aspx"); 
    Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //       
    Response.AddHeader("Content-Disposition","attachment;filename=index.html"); //            
    Response.AddHeader("Content-length",str.Length.ToString()); 
    Response.Write(str); 
    Response.End(); 
} 

以上asp.Netはホームページのソースコードのすべてのコードを捕まえて、みんなに役に立つことを望みます.