如何用.Net 取得指定网面的内容? How to get html web page data?

来源:互联网 发布:php socket例子 编辑:程序博客网 时间:2024/05/16 11:32

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

namespace Examples.System.Net
{
    public class WebRequestGetExample
    {
        public static void Main()
        {

            // Create a request for the URL.
            WebRequest request = WebRequest.Create(
              "http://www.microsoft.com/default.aspx");

            WebProxy pry = new WebProxy("192.168.1.1", 8080);

            //The DefaultCredentials automically get username and password.
            pry.Credentials = CredentialCache.DefaultCredentials;
            //GlobalProxySelection.Select = pry;
            request.Proxy = pry;
            // If required by the server, set the credentials.
            request.Credentials = CredentialCache.DefaultCredentials;
            // Get the response.
            WebResponse response = request.GetResponse();
            // Display the status.
            Console.WriteLine(((HttpWebResponse)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);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Display the content.
            Console.WriteLine(responseFromServer);
            // Clean up the streams and the response.
            reader.Close();
            response.Close();
            // stop the console screen for user check data
            Console.ReadLine();
        }
    }
}

原创粉丝点击