zoukankan      html  css  js  c++  java
  • C#获取网页源码

    WebClient

    	private string GetWebClient(string url)
    		{
    			string strHTML = "";
    			WebClient myWebClient = new WebClient();
    			Stream myStream = myWebClient.OpenRead(url);
    			StreamReader sr = new StreamReader(myStream, System.Text.Encoding.GetEncoding(this.txtEncoder.Text));
    			strHTML = sr.ReadToEnd();
    			myStream.Close();
    
    			return strHTML;
    		}
    

    WebRequest

    private string GetWebRequest(string url)
    		{
    			Uri uri = new Uri(url);
    			WebRequest myReq = WebRequest.Create(uri);
    			WebResponse result = myReq.GetResponse();
    			Stream receviceStream = result.GetResponseStream();
    			StreamReader readerOfStream = new StreamReader(receviceStream, System.Text.Encoding.GetEncoding(this.txtEncoder.Text));
    			string strHTML = readerOfStream.ReadToEnd();
    			readerOfStream.Close();
    			receviceStream.Close();
    			result.Close();
    
    			return strHTML;
    		}
    

    HttpWebRequest

    private string GetHttpWebRequest(string url)
    {
        Uri uri = new Uri(url);
        HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(uri);
        myReq.UserAgent = "User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705";
        myReq.Accept = "*/*";
        myReq.KeepAlive = true;
        myReq.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.5");
        HttpWebResponse result = (HttpWebResponse)myReq.GetResponse();
        Stream receviceStream = result.GetResponseStream();
        StreamReader readerOfStream = new StreamReader(receviceStream, System.Text.Encoding.GetEncoding("utf-8"));
        string strHTML = readerOfStream.ReadToEnd();
        readerOfStream.Close();
        receviceStream.Close();
        result.Close();
        return strHTML;
    }
    
  • 相关阅读:
    第8章 字符串
    第7章 方法
    第6章 类和对象
    第5章 数组
    第4章 循环结构、break与continue
    第3章 选择结构
    第2章 变量、数据类型和运算符
    Codeforces Round #426 (Div. 2)
    CCNA学前基础一
    Tinkoff Challenge
  • 原文地址:https://www.cnblogs.com/lampon/p/3547765.html
Copyright © 2011-2022 走看看