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();
            }
       也在网上搜了些资料,但还是找不到更好的办法,有懂的吗?知道的回复一下,谢谢!
     
  • 相关阅读:
    Flex 布局语法教程
    2017年总结的前端文章——border属性的多方位应用和实现自适应三角形
    html 里 checkbox里 只要选中就会自动添加checked=“checked”么?
    jQuery遍历DOM
    checkbox 全选操作
    ubuntu下安装jdk
    ubuntu下安装nodejs
    nodejs express route 的用法
    聊天室业务分析
    一般使用场景
  • 原文地址:https://www.cnblogs.com/SanMaoSpace/p/2118128.html
Copyright © 2011-2022 走看看