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(); } }
C# Network
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"); } }
Download Data Test
using System; using System.Net; using System.Text; public class DownloadDataTest { public static void Main(string[] argv) { WebClient wc = new WebClient(); byte[] response = wc.DownloadData(argv[0]); Console.WriteLine(Encoding.ASCII.GetString(response)); } }
NetworkCredential test
using System; using System.Net; using System.Text; public class CredTest { public static void Main() { WebClient wc = new WebClient(); NetworkCredential nc = new NetworkCredential("alex", "mypassword"); wc.Credentials = nc; byte[] response = wc.DownloadData("http://www.kutayzorlu.com/java2s/com/index.htm"); Console.WriteLine(Encoding.ASCII.GetString(response)); } }