C# .Netはどのようにホームページの内容をつかみます

7212 ワード

ASP.NET              ,         ASP           。

1、      

     :WebRequest、WebResponse、StreamReader

      :System.Net、System.IO

    :

      WebRequest    Create      ,            ;
      Encoding     ,Encoding      ASCII、UTF32、UTF8         ,    gb2312       ,       GetEncoding    gb2312   。
[csharp] view plaincopy CODE              
private string GetGeneralContent(string strUrl)  
    {  
        string strMsg = string.Empty;  
        try  
        {  
            WebRequest request = WebRequest.Create(strUrl);  
            WebResponse response = request.GetResponse();  
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));  
  
            strMsg = reader.ReadToEnd();  
  
            reader.Close();  
            reader.Dispose();  
            response.Close();  
        }  
        catch  
        { }  
        return strMsg;  
    }  
2、            (   )      :WebRequest、WebResponse、Stream、FileStream       :System.Net、System.IO     : Stream  
[csharp] view plaincopy CODE              
private string GetFileContent(string strUrl)  
    {  
        string strMsg = string.Empty;  
        try  
        {  
            WebRequest request = WebRequest.Create(strUrl);   
            WebResponse response = request.GetResponse();   
            Stream reader = response.GetResponseStream();   
              
            //              
            FileStream writer = new FileStream("D:\\logo.gif", FileMode.OpenOrCreate, FileAccess.Write);   
            byte[] buff = new byte[512];   
            int c = 0; //           
            while ((c=reader.Read(buff, 0, buff.Length)) > 0)   
            {   
                writer.Write(buff, 0, c);   
            }   
            writer.Close();   
            writer.Dispose();   
               
            reader.Close();   
            reader.Dispose();   
            response.Close();  
  
            strMsg = "    ";  
        }  
        catch  
        { }  
        return strMsg;  
    }  

3、       POST         ,   ,          Post          ,                ,           Post     :
[csharp] view plaincopy CODE              
private string GetPostContent(string strUrl)  
    {  
        string strMsg = string.Empty;  
        try  
        {  
            string data = "userName=admin&passwd=admin888";   
            byte[] requestBuffer = System.Text.Encoding.GetEncoding("gb2312").GetBytes(data);  
  
            WebRequest request = WebRequest.Create(strUrl);  
            request.Method = "POST";   
            request.ContentType = "application/x-www-form-urlencoded";   
            request.ContentLength = requestBuffer.Length;  
            using (Stream requestStream = request.GetRequestStream())   
            {   
                requestStream.Write(requestBuffer, 0, requestBuffer.Length);   
                requestStream.Close();   
            }  
  
            WebResponse response = request.GetResponse();  
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312")))   
            {  
                strMsg = reader.ReadToEnd();   
                reader.Close();   
            }  
        }  
        catch  
        { }  
        return strMsg;  
    }  

4、ASP.NET       -            ,            ,          Response.Redirect         ,            ,  ,       reader.ReadToEnd()   Response.Write   ,    。 5、ASP.NET       -          Post               ,             ,             Request        。   ,      HttpWebRequest,    WebRequest。   WebRequest   ,      :
[csharp] view plaincopy CODE              
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(strUrl);  

  :HttpWebRequest.Create         WebRequest,       。   ,   CookieContainer。
[csharp] view plaincopy CODE              
System.Net.CookieContainer cc = new System.Net.CookieContainer();   
request.CookieContainer = cc;   
request2.CookieContainer = cc;  

   request   request2           Session,   request    ,   request2       。

   ,               CookieContainer。

               CookieContainer,    CookieContainer    Session。
[csharp] view plaincopy CODE              
Session.Add("ccc", cc); //    
CookieContainer cc = (CookieContainer)Session["ccc"]; // Session  
[csharp] view plaincopy CODE              
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(strUrl);  
              
            //      
            //HttpWebRequest request2 = (HttpWebRequest)HttpWebRequest.Create(strUrl);  
            //System.Net.CookieContainer cc = new CookieContainer();  
            //request.CookieContainer = cc;  
            //request2.CookieContainer = cc;  
            //    ,        request,request2    
            object obj = Session["ccc"];  
            if (obj == null)  
            {  
                CookieContainer cc = new CookieContainer();  
                //requestr     
                Session.Add("ccc", cc);  
            }  
            string strUrl2 = "";  
            HttpWebRequest request2 = (HttpWebRequest)HttpWebRequest.Create(strUrl2);  
            //     
            CookieContainer cc2 = (CookieContainer)Session["ccc"];  
            request2.CookieContainer = cc2;  
            //          

6、ASP.NET       -        WebRequest  
        B1         S1,        ,      S2    WebRequest         S1,         。        WebRequest       B1   S1      ,       S1     B1     S1,    S2     S1。
      Cookie  ,   S1      B1   SessionID   Cookie,     Cookie    S2,S2    Cookie    WebRequest  。
[csharp] view plaincopy CODE              
WebRequest request = WebRequest.Create("url");  
 request.Headers.Add(HttpRequestHeader.Cookie, "ASPSESSIONIDSCATBTAD=KNNDKCNBONBOOBIHHHHAOKDM;");  
 WebResponse response = request.GetResponse();  
 StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312"));  
 Response.Write(reader.ReadToEnd());  
 reader.Close();  
 reader.Dispose();  
 response.Close();  

     :
          Cookie   ,   SessionID   S1    S2  ,    S2    ,      ,                  。
    S1      B1   Session,   SessionID       Cookie  ,   SessionID       。
      ASP.NET    Cookie   Request.Cookies,     Cookie      。
             ,SessionID   Cookie         ,    ASP   SessionID。
    S1         SessionID        ,         Referer、User-Agent  ,     S1       。
              “      ”      。
7、ASP.NET       -       Referer   UserAgent
[csharp] view plaincopy CODE              
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://127.0.0.1/index.htm");   
request.Referer = "http://www.csdn.net/";  
request.UserAgent = "      ";  
//