To invoke an XML Web service asynchronously

Source: Internet
Author: User
Tags foreach end net readline string tostring
web|xml| Asynchronous
Using System;
Using System.Net;
Using System.IO;
Using System.Text;
Using System.Xml;
Using System.Collections.Specialized;
Using System.Diagnostics;

Namespace Technology
... {
/**////<summary>
Summary description for Main.
</summary>
Class Tester
... {
public void callback (IAsyncResult ar) ... {
HttpWebRequest req= (HttpWebRequest) ar. asyncstate;
HttpWebResponse res= (HttpWebResponse) req. EndGetResponse (AR);
Console.WriteLine ("* * * Response Start * * *");
Console.WriteLine (Res. Statuscode.tostring ());
Console.WriteLine (Res. Statusdescription);
Displayheaders
foreach (String Sitem in Res. Headers)
... {
Console.WriteLine (Sitem + ":" + Res. Headers[sitem]);
}
Stream stream = Res. GetResponseStream ();
if (stream!= null)
... {
StreamReader sr = new StreamReader (stream, encoding.ascii);
Console.WriteLine (Sr. ReadToEnd ());
}
Console.WriteLine ("* * * Response END * * * *");

Console.ReadLine ();

}
static void Main (string[] args)
... {


1.construct Request


HttpWebRequest req = (HttpWebRequest) webrequest.create ("Http://localhost/WebService2/Service1.asmx/HelloWorld");
Req. method = "POST";
Req. Contenttype= "application/x-www-form-urlencoded";
String content= "name=100";
Req. Contentlength=content. Length;
Stream s = req. GetRequestStream ();
StreamWriter sw = new StreamWriter (s);
Sw. Write (content);
Sw. Close ();
Tester t=new Tester ();
AsyncCallback callback=new AsyncCallback (T.callback);
Req. BeginGetResponse (Callback,req);

2.Display Request
Console.WriteLine ("* * Request Start * * * *");
Console.WriteLine (req. Requesturi.tostring ());
foreach (String Sitem in req. Headers)
... {
Console.WriteLine (Sitem + ":" +req. Headers[sitem]);
}
Console.WriteLine ("* * * Request END * * * *");
Console.ReadLine ();
}
}





}






Using System;
Using System.Net;
Using System.IO;
Using System.Text;
Using System.Xml;
Using System.Collections.Specialized;
Using System.Diagnostics;

Namespace Technology
... {
/**////<summary>
Summary description for Main.
</summary>
Class Tester
... {

static void Main (string[] args)
... {


1.construct Request

HttpWebRequest req = (HttpWebRequest) webrequest.create ("Http://218.193.118.9/WebService4/Service1.asmx/HelloWorld ");
Req. method = "POST";
Req. Contenttype= "application/x-www-form-urlencoded";
String content= "slowtime=5000";
Req. Contentlength=content. Length;
Stream s = req. GetRequestStream ();
StreamWriter sw = new StreamWriter (s);
Sw. Write (content);
Sw. Close ();

2.Display Request
Console.WriteLine ("* * Request Start * * * *");
Console.WriteLine (req. Requesturi.tostring ());
Displayheaders
foreach (String Sitem in req. Headers)
... {
Console.WriteLine (Sitem + ":" +req. Headers[sitem]);
}
Console.WriteLine ("* * * Request END * * * *");


3.get response


HttpWebResponse res = (HttpWebResponse) req. GetResponse ();


4.DisplayResponse
Console.WriteLine ("* * * Response Start * * *");
Console.WriteLine (Res. Statuscode.tostring ());
Console.WriteLine (Res. Statusdescription);
Displayheaders
foreach (String Sitem in Res. Headers)
... {
Console.WriteLine (Sitem + ":" + Res. Headers[sitem]);
}
Displaycontent
Stream stream = Res. GetResponseStream ();
if (stream!= null)
... {
StreamReader sr = new StreamReader (stream, encoding.ascii);
Console.WriteLine (Sr. ReadToEnd ());
}
Console.WriteLine ("* * * Response END * * * *");

Console.ReadLine ();
}
}
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.