chapter 14 HTTP >>> 535
Part 3 _ 1 L i Sting using System; using System.Net; using System.Text; class DownloadDataTest public static void Main (string[] argv) WebClient wc = new WebClient(); byte[] response = wc.downloaddata(argv[0]); 536
Console.WriteLine(Encoding.ASCII.GetString(response)); C:\>DownloadDataTest http://localhost/test.htm <html> <title>this is a test web page</title> <body> <h1>this is a test web page</h1> </body> </html> C:\ > N o t e L i Sting using System; using System.Net; class DownloadFileTest 537
Part 3 _ public static void Main (string[] argv) WebClient wc = new WebClient(); string filename = "webpage.htm"; wc.downloadfile(argv[0], filename); Console.WriteLine("file downloaded"); C:\>DownloadFileTest http://localhost/test.htm file downloaded C:\>type webpage.htm <html> <title>this is a test web page</title> <body> <h1>this is a test web page</h1> </body> </html> C:\> L i Sting using System; using System.IO; using System.Net; class OpenReadTest public static void Main (string[] argv) 538
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(); L i Sting using System; using System.Collections.Specialized; using System.Net; class ResponseHeadersTest public static void Main (string[] argv) WebClient wc = new WebClient(); 539
Part 3 _ 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)); C:\>ResponseHeadersTest http://www.microsoft.com header count = 10 Date = Tue, 27 Aug 2002 04:06:34 GMT Server = Microsoft-IIS/6.0 P3P = CP='ALL IND DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo CNT COM INT NAV ONL PHY PRE PUR UNI' Content-Length = 31257 Content-Type = text/html Expires = Tue, 27 Aug 2002 18:06:34 GMT Cache-Control = private Age = 0 X-Cache = MISS from proxy.ispnet.net Proxy-Connection = keep-alive C:\ > 540
W a r ning OpenWrite(string URI); OpenWrite(string URI, string method); L i Sting using System; using System.IO; using System.Net; 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(); 541
Part 3 _ UploadData(string URI, byte[] array); UploadData(string URI, string method, byte[] array); L i Sting using System; using System.Net; using System.Text; 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); UploadFile(string URI, string filename); UploadFile(string URI, string method, string filename); 542
?lastname=blum &firstname=rich NameValueCollection nvc = new NameValueCollection(); nvc.add("lastname", "Blum"); nvc.add("firstname", "Rich"); byte[] response = wc.uploadvalues(uri, "POST", nvc); N o t e L i Sting using System; using System.Collection.Specialized; using System.Net; using System.Text; class UploadValuesTest public static void Main (string[] argv) WebClient wc = new WebClient(); string uri = "http://localhost/testform.aspx"; // 543
Part 3 _ NameValueCollection nvc = new NameValueCollection(); nvc.add("lastname", "Blum"); nvc.add("firstname", "Rich"); byte[] response = wc.uploadvalues(uri, nvc); Console.WriteLine(Encoding.ASCII.GetString(response)); NetworkCredential() NetworkCredential(string username, string password) NetworkCredential(string username, string password, string domain) 544
L i Sting using System; using System.Net; using System.Text; class CredTest public static void Main() WebClient wc = new WebClient(); NetworkCredential nc = new NetworkCredential("alex", "mypassword"); wc.credentials = nc; byte[] response = wc.downloaddata("http://localhost/testlogin"); Console.WriteLine(Encoding.ASCII.GetString(response)); Add(URI website, string authtype, NetworkCredential cred) 545
Part 3 _ L i Sting using System; using System.Net; using System.Text; class CredCacheTest public static void Main() WebClient wc = new WebClient(); string website1 = "http://remote1.ispnet.net"; string website2 = "http://remote2.ispnet.net"; string website3 = "http://remote3.ispnet.net/login"; NetworkCredential nc1 = new NetworkCredential("mike", "guitars"); NetworkCredential nc2 = new NetworkCredential ("evonne", "singing", "home"); NetworkCredential nc3 = new NetworkCredential("alex", "drums"); CredentialCache cc = new CredentialCache(); cc.add(new Uri(website1), "Basic", nc1); cc.add(new Uri(website2), "Basic", nc2); cc.add(new Uri(website3), "Digest", nc3); wc.credentials = cc; wc.downloadfile(website1, "website1.htm"); wc.downloadfile(website2, "website2.htm"); wc.downloadfile(website3, "website3.htm"); 546
2 HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(http://remotehost/webpage.htm); 547
Part 3 _ HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create( "http://remote1.ispnet2.net); WebProxy proxysrv = new WebProxy("http://proxy1.ispnet.net:2000"); hwr.proxy = proxysrv; HttpWebrequest hwr = (HttpWebRequest)WebRequest.Create("http://localhost"); Stream strm = hwr.getrequeststream(); StreamWriter sw = new StreamWriter(strm); sw.writeline(data); W a r ning 548
549
Part 3 _ String header = hwr.getresponseheader("x-cache"); HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create("http://www.amazon.com"); hwr.cookiecontainer = new CookieContainer(); HttpWebResponse hwrsp = (HttpWebResponse)hwr.GetResponse(); hwrsp.cookies = hwr.cookiecontainer.getcookies(hwr.requesturi); foreach(cookie cky in hwrsp.cookies) Console.WriteLine(cky.Name + " = " + cky.value); 550
L i Sting using System; using System.Drawing; using System.IO; using System.Net; using System.Windows.Forms; class WebGet : Form private TextBox uribox; private ListBox headers; private ListBox cookies; private ListBox response; public WebGet() Text = "WebGet - a web page retriever"; Size = new Size(500, 450); Label label1 = new Label(); label1.parent = this; label1.text = "URI:"; label1.autosize = true; label1.location = new Point(10, 23); uribox = new TextBox(); uribox.parent = this; uribox.size = new Size(200, 2 * Font.Height); uribox.location = new Point(35, 20); Label label2 = new Label(); label2.parent = this; label2.text = "Headers:"; label2.autosize = true; label2.location = new Point(10, 46); 551
Part 3 _ headers = new ListBox(); headers.parent = this; headers.horizontalscrollbar = true; headers.location = new Point(10, 65); headers.size = new Size(450, 6 * Font.Height); Label label3 = new Label(); label3.parent = this; label3.text = "Cookies:"; label3.autosize = true; label3.location = new Point(10, 70 + 6 * Font.Height); cookies = new ListBox(); cookies.parent = this; cookies.horizontalscrollbar = true; cookies.location = new Point(10, 70 + 7 * Font.Height); cookies.size = new Size(450, 6 * Font.Height); Label label4 = new Label(); label4.parent = this; label4.text = "HTML:"; label4.autosize = true; label4.location = new Point(10, 70 + 13 * Font.Height); response = new ListBox(); response.parent = this; response.horizontalscrollbar = true; response.location = new Point(10, 70 + 14 * Font.Height); response.size = new Size(450, 12 * Font.Height); Button sendit = new Button(); sendit.parent = this; sendit.text = "GetIt"; sendit.location = new Point(275, 18); sendit.size = new Size(7 * Font.Height, 2 * Font.Height); sendit.click += new EventHandler(ButtongetitOnClick); void ButtongetitOnClick(object obj, EventArgs ea) headers.items.clear(); cookies.items.clear(); response.items.clear(); 552
HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(uribox.Text); hwr.cookiecontainer = new CookieContainer(); HttpWebResponse hwrsp = (HttpWebResponse)hwr.GetResponse(); WebHeaderCollection whc = hwrsp.headers; for (int i = 0; i < whc.count; i++) headers.items.add(whc.getkey(i) + " = " + whc.get(i)); hwrsp.cookies = hwr.cookiecontainer.getcookies(hwr.requesturi); foreach(cookie cky in hwrsp.cookies) cookies.items.add(cky.name + " = " + cky.value); Stream strm = hwrsp.getresponsestream(); StreamReader sr = new StreamReader(strm); while (sr.peek() > -1) response.items.add(sr.readline()); sr.close(); strm.close(); public static void Main() Application.Run(new WebGet()); 553
Part 3 _ W a r ning 3 N o t e 554
555
Part 3 _ L i Sting <%@ WebService Language="c#" Class="MathService"%> using System; using System.Web.Services; [WebService(Namespace="http://localhost/test")] public class MathService : WebService [WebMethod] public int Add(int a, int b) int answer; answer = a + b; return answer; [WebMethod] public int Subtract(int a, int b) int answer; answer = a - b; return answer; [WebMethod] public int Multiply(int a, int b) int answer; answer = a * b; return answer; [WebMethod] public int Divide(int a, int b) int answer; if (b!= 0) answer = a / b; return answer; else return 0; 556
<%@ WebService Language="c#" Class="MathService"%> [WebService(Namespace=http://localhost/test)] C:\Inetpub\wwwroot\test\MathService.asmx 557
Part 3 _ C:\>wsdl http://localhost/test/mathservice.asmx csc /t:library MathService.cs 558
L i Sting using System; class ServiceTest public static void Main (string[] argv) MathService ms = new MathService(); int x = Convert.ToInt16(argv[0]); int y = Convert.ToInt16(argv[1]); int sum = ms.add(x, y); int sub = ms.subtract(x, y); int mult = ms.multiply(x, y); int div = ms.divide(x, y); Console.WriteLine("The answers are:"); Console.WriteLine(" 0 + 1 = 2", x, y, sum); Console.WriteLine(" 0-1 = 2", x, y, sub); Console.WriteLine(" 0 * 1 = 2", x, y, mult); Console.WriteLine(" 0 / 1 = 2", x, y, div); csc /r:mathservice.dll ServiceTest.cs 559
Part 3 _ C:\>ServiceTest 100 50 The answers are: 100 + 50 = 150 100-50 = 50 100 * 50 = 5000 100 / 50 = 2 C:\> 4 560
561