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);// 注:汉字需要转为UTF8格式
                // 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); // 注:汉字需要转为UTF8格式
                string s = reader.ReadToEnd();
                Response.Write(s);
                data.Close();
                reader.Close();
               
                //方法三:
                 WebClient client = new WebClient();
                //client.DownloadFile("http://www.hao123.com%22,%22123.htm/");
                string reply = client.DownloadString("http://www.hao123.com/");
                Response.Write(reply);

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wxg22526451/archive/2008/03/24/2213841.aspx

  • 相关阅读:
    java 下载图片并传输(java自带 BASE64工具进行图片和字符串转换)
    MySQL的日期格式
    eclipse下查看maven下载的源码中文乱码问题
    Linux----部署
    python----logging
    python----pymysql
    vmware15 激活秘钥
    vmware15 激活秘钥
    Ubuntu18.04安装
    msyql45讲 20--幻读是什么,幻读有什么问题?
  • 原文地址:https://www.cnblogs.com/chen1987lei/p/1682117.html
Copyright © 2011-2022 走看看