日期:2014-04-06  浏览次数:20902 次

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