老外编的程序(二)--System.Net的示例程序
点击次数:34 次 发布日期:2008-11-27 01:17:05 作者:源代码网
|
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."); } } } &n 源代码网供稿. |
