异步调用xml web service

 

  
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();
         }

     }
    
}

 

你可能感兴趣的:(xml,Web,String,Stream,service,callback)