WebClient位于System.Net命名空间下,通过这个类可以方便的创建Http请求并获取返回内容。

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace ConsoleWebClient
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
string uri = "http://www.cnblogs.com";
            WebClient wc 
= new WebClient();
            Stream st 
= wc.OpenRead(uri);
            StreamReader sr 
= new StreamReader(st);
            
string res = sr.ReadToEnd();
            sr.Close();
            st.Close();
            Console.WriteLine(res);
            Console.ReadKey();
        }

        
public void ReadPageContent()
        {
            
string uri = "http://www.cnblogs.com";
            WebClient wc 
= new WebClient();
            
byte[] bResponse = wc.DownloadData(uri);
            
string strResponse = Encoding.UTF8.GetString(bResponse);
            Console.WriteLine(strResponse);
            Console.ReadKey();
        }
    }
}

 

posted @ 2010-03-29 10:10  smodi  阅读(2050)  评论(0编辑  收藏  举报