老外编的程序(二)--System.Net的示例程序

发表于:2007-06-30来源:作者:点击数: 标签:
using System; using System.Net; using System.Text; using System.Collections.Specialized; public class WebClientSample { public static void Main() { try { // Download the data to a buffer WebClient client = new WebClient(); Byte[] pageData =
using System;
using System.Net;
using System.Text;
using System.Collections.Specialized;

public class WebClientSample
{
    public static void Main()
    {
        try {
            
        // Download the data to a buffer
               WebClient client = new WebClient();

          Byte[] pageData = client.DownloadData("http://www.microsoft.com");
        string pageHtml = Encoding.ASCII.GetString(pageData);
        Console.WriteLine(pageHtml);

        // Download the data to a file
                client.DownloadFile("http://www.bn.com", "page.htm");

        // Upload some form post values
        NameValueCollection form = new NameValueCollection();        
        form.Add("MyName", "MyValue");        
        Byte[] responseData = client.UploadValues("http://localhost/somefile.aspx", form);        

        }
        catch (WebException webEx) {
              Console.WriteLine(webEx.ToString());
               if(webEx.Status == WebExceptionStatus.ConnectFailure) {
                   Console.WriteLine("Are you behind a firewall?  If so, go through the proxy server.");
               }
        }
    }        
}

原文转自:http://www.ltesting.net