如何对HttpWebRequest和HttpWebRsponse异步调用?

简介:

如何对HttpWebRequest和HttpWebRsponse异步调用?

public void Post(string url)
 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.KeepAlive = true;
            req.Timeout = 300000;
            req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
            postData = Encoding.UTF8.GetBytes(PostData(param));
 req.BeginGetRequestStream(new AsyncCallback(RequestStreamCallBack), req);
  }
   public static void RequestStreamCallBack(IAsyncResult result)
        {
            HttpWebRequest request = (HttpWebRequest)result.AsyncState;
            Stream reqStream = request.EndGetRequestStream(result);
            reqStream.Write(postData, 0, postData.Length);
            reqStream.Close();
            //如何让程序在此处步不回到界面,调用完下面的对流的读取后,在返回界面?谢谢高手指点!
            request.BeginGetResponse(new AsyncCallback(ResponseCallBack), request);
        }
        public static void ResponseCallBack(IAsyncResult result)
        {
            HttpWebRequest req = (HttpWebRequest)result.AsyncState;
            HttpWebResponse response = (HttpWebResponse)req.EndGetResponse(result);
 
            using (Stream sw = response.GetResponseStream())
            {
                using (StreamReader reader = new StreamReader(sw))
                {
                    xmls = reader.ReadToEnd();
                }
            }
            if (response != null) response.Close();
 
        }

 
 
相关文章
|
Android开发
【OkHttp】OkHttp Get 和 Post 请求 ( 同步 Get 请求 | 异步 Get 请求 | 同步 Post 请求 | 异步 Post 请求 )(一)
【OkHttp】OkHttp Get 和 Post 请求 ( 同步 Get 请求 | 异步 Get 请求 | 同步 Post 请求 | 异步 Post 请求 )(一)
994 0
|
5月前
|
前端开发 JavaScript API
Promise封装Ajax请求
Promise封装Ajax请求
29 0
|
C# 数据格式 XML
C# 使用 HttpPost 请求调用 WebService
原文:C# 使用 HttpPost 请求调用 WebService 之前调用 WebService 都是直接添加服务引用,然后调用 WebService 方法的,最近发现还可以使用 Http 请求调用 WebService。
2771 0
【OkHttp】OkHttp Get 和 Post 请求 ( 同步 Get 请求 | 异步 Get 请求 | 同步 Post 请求 | 异步 Post 请求 )(二)
【OkHttp】OkHttp Get 和 Post 请求 ( 同步 Get 请求 | 异步 Get 请求 | 同步 Post 请求 | 异步 Post 请求 )(二)
318 0
|
Web App开发 .NET Windows
WebApi 异步请求(HttpClient)
还是那几句话: 学无止境,精益求精 十年河东,十年河西,莫欺少年穷 学历代表你的过去,能力代表你的现在,学习代表你的将来 废话不多说,直接进入正题: 今天公司总部要求各个分公司把短信接口对接上,所谓的短信接口其实就是GET或者Post请求,接到这个任务感觉好Easy。
1020 0
|
前端开发
第106天:Ajax中同步请求和异步请求
同步请求和异步请求的区别 1、同步是指:发送方发出数据后,等接收方发回响应以后才发下一个数据包的通讯方式。 用户填写所有信息后,提交给服务器,等待服务器的回应(检验数据),是一次性的。信息错误又要重新填写! 2、异步是指:发送方发出数据后,不等接收方发回响应,接着发送下个数据包的通讯方式。
1322 0