zoukankan      html  css  js  c++  java
  • 如何对HttpWebRequest异步调用?

        public static ManualResetEvent allDone = new ManualResetEvent(false);
            static void Main(string[] args)
            {          
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.contoso.com/example.aspx");
     
                request.ContentType = "application/x-www-form-urlencoded";
                request.Method = "POST";  
                request.BeginGetRequestStream(new AsyncCallback(ReadCallback), request);
     
                allDone.WaitOne();
     
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream streamResponse = response.GetResponseStream();
                StreamReader streamRead = new StreamReader(streamResponse);
                string responseString = streamRead.ReadToEnd();
     
                streamResponse.Close();
                streamRead.Close();
                response.Close();
                Console.WriteLine(responseString);
                Console.ReadKey();
            }
            private static void ReadCallback(IAsyncResult asynchronousResult)
            {
                HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
                Stream postStream = request.EndGetRequestStream(asynchronousResult);
                Console.WriteLine("Please enter the input data to be posted:");
                string postData = Console.ReadLine();
     
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                postStream.Write(byteArray, 0, postData.Length);
                postStream.Close();
                allDone.Set();
            }
       也在网上搜了些资料,但还是找不到更好的办法,有懂的吗?知道的回复一下,谢谢!
     
  • 相关阅读:
    hdu 1164 Eddy's research I
    hdu 3794 Magic Coupon
    hdu 1460 完数
    hdu 1201 18岁生日
    求一组整数中所有素数之和
    备忘录
    c判断括弧是否匹配
    N!大整数阶乘问题
    计算一个人从出生到现在活了多少天
    java web.xml配置详解(转)
  • 原文地址:https://www.cnblogs.com/SanMaoSpace/p/2118128.html
Copyright © 2011-2022 走看看