zoukankan      html  css  js  c++  java
  • C#获取网页内容

    using System.Net;
    using System.IO;
    using System.Text;


           
                //方法一:
                // Create a request for the URL.         
                WebRequest request = WebRequest.Create("http://www.hao123.com");
                // If required by the server, set the credentials.
                request.Credentials = CredentialCache.DefaultCredentials;
                // Get the response.
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                // Display the status.
                Response.Write(response.StatusDescription);
                // Get the stream containing content returned by the server.
                Stream dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader(dataStream,Encoding.Default);
                // Read the content.
                string responseFromServer = reader.ReadToEnd();
                // Display the content.
                Response.Write(responseFromServer);
                // Cleanup the streams and the response.
                reader.Close();
                dataStream.Close();
                response.Close();

                //方法二:
                WebClient client = new WebClient();

                // Add a user agent header in case the 
                // requested URI contains a query.

                client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

                Stream data = client.OpenRead("http://www.hao123.com");
                StreamReader reader = new StreamReader(data,Encoding.Default);
                string s = reader.ReadToEnd();
                Response.Write(s);
                data.Close();
                reader.Close();
                
                //方法三:
                 WebClient client = new WebClient();
                //client.DownloadFile("http://www.hao123.com","123.htm");
                string reply = client.DownloadString("http://www.hao123.com");
                Response.Write(reply);

  • 相关阅读:
    Android应用开发提高系列(1)——《Practical Java 中文版》读书笔记(上)
    Android开发指南(35) —— Toast Notifications
    Android开发指南(37) —— Data Backup
    Android开发指南(36) —— Search
    [转]闲话操作系统1
    [从架构到设计]第二回:对象的旅行对象和人,两个世界,一样情怀
    [Thinking]从赢在中国,思考博客园的商业化
    [你必须知道的.NET]目录导航
    070809
    [CLR团队公告]CLR基础研究团队纲领
  • 原文地址:https://www.cnblogs.com/sungcong/p/1909009.html
Copyright © 2011-2022 走看看