using System; using System.Collections.Generic; using System.Text; using System.Net; class Program { static void Main(string[] args) { WebClient client = new WebClient(); client.BaseAddress = "http://www.microsoft.com"; string data = client.DownloadString("Office"); Console.WriteLine(data); } }
Web Client Open Read Test
using System; using System.IO; using System.Net; public class OpenReadTest { public static void Main(string[] argv) { WebClient wc = new WebClient(); string response; Stream strm = wc.OpenRead(argv[0]); StreamReader sr = new StreamReader(strm); while(sr.Peek() > -1) { response = sr.ReadLine(); Console.WriteLine(response); } sr.Close(); } }
Web Client Open Write Test
using System; using System.IO; using System.Net; public class OpenWriteTest { public static void Main(string[] argv) { WebClient wc = new WebClient(); string data = "Data up upload to server"; Stream strm = wc.OpenWrite(argv[0]); StreamWriter sw = new StreamWriter(strm); sw.WriteLine(data); sw.Close(); strm.Close(); } }
Web Client Response Headers Test
using System;
using System.Collections.Specialized;
using System.Net;
public class ResponseHeadersTest
{
public static void Main(string[] argv)
{
WebClient wc = new WebClient();
byte[] response = wc.DownloadData(argv[0]);
WebHeaderCollection whc = wc.ResponseHeaders;
Console.WriteLine(“header count = {0}”, whc.Count);
for (int i = 0; i < whc.Count; i++)
{
Console.WriteLine(whc.GetKey(i) + " = " + whc.Get(i));
}
}
}
[/csharp]
Web Client Upload Data Test 2
using System; using System.Net; using System.Text; public class UploadDataTest { public static void Main(string[] argv) { WebClient wc = new WebClient(); string data = "This is the data to post"; byte[] dataarray = Encoding.ASCII.GetBytes(data); wc.UploadData(argv[0], dataarray); } }
Web Client Upload Values Test
using System; using System.Collections.Specialized; using System.Net; using System.Text; public class UploadValuesTest { public static void Main(string[] argv) { WebClient wc = new WebClient(); string uri = "http://localhost/testform.aspx"; NameValueCollection nvc = new NameValueCollection(); nvc.Add("lastname", "Blum"); nvc.Add("firstname", "Rich"); byte[] response = wc.UploadValues(uri, nvc); Console.WriteLine(Encoding.ASCII.GetString(response)); } }
Download File Test
using System; using System.Net; public class DownloadFileTest { public static void Main(string[] argv) { WebClient wc = new WebClient(); string filename = "webpage.htm"; wc.DownloadFile(argv[0], filename); Console.WriteLine("file downloaded"); } }