.NET线程 - HttpWebRequest的BeginGetResponse +的AutoResetEvent [英] .NET Threading - HttpWebRequest BeginGetResponse + AutoResetEvent

查看:141
本文介绍了.NET线程 - HttpWebRequest的BeginGetResponse +的AutoResetEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这两个之间的办法是更好地执行?
我需要创建一个Web请求,可以200毫秒到5秒之间。我需要的HTML响应进行 - 因此需要在主线程阻塞



第一种方法



 字符串的GetResponse()
{

HttpWebRequest的要求=(HttpWebRequest的)HttpWebRequest.Create(URL);
IAsyncResult的结果= request.BeginGetResponse(NULL,NULL);

使用(HttpWebResponse的HttpResponse =(HttpWebResponse)request.EndGetResponse(结果))使用(流数据流= httpResponse.GetResponseStream())
{
$ B {
$ b StreamReader的读者=新的StreamReader(数据流);
响应= reader.ReadToEnd();
}
}



第二种方法

 字符串响应=的String.Empty; 
的AutoResetEvent的WaitHandle = NULL;
无效的GetResponse(字符串URL)
{
的WaitHandle =新的AutoResetEvent(假);

{
HttpWebRequest的要求=(HttpWebRequest的)HttpWebRequest.Create(URL);
IAsyncResult的asyncResult = request.BeginGetResponse(回调,请求);

waitHandle.WaitOne();
}
赶上{}
终于
{
waitHandle.Close();
}
}

的回调(IAsyncResult的asyncResult)
{

HttpWebRequest的要求=(HttpWebRequest的)asyncResult.AsyncState;

{使用(HttpWebResponse的HttpResponse =(HttpWebResponse)request.EndGetResponse(asyncResult))
{
如果(httpResponse.StatusCode == HttpStatusCode.OK)$
b $ b {使用
(流数据流= httpResponse.GetResponseStream())
{
StreamReader的读者=新的StreamReader(数据流);
响应= reader.ReadToEnd();
}
}
}
}
赶上{}
终于
{
waitHandle.Set();
}
}


解决方案

为什么不执行主线程上的Web请求?如果你想在主线程被阻塞,这是迄今为止做到这一点最简单的方法。


I would like to know which approach among the two is a better implementation ? I need to create a web request which can range between 200ms to 5 seconds. I need the html response to proceed - so need to block on the main thread.

First Approach

string GetResponse()
{

    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
    IAsyncResult result = request.BeginGetResponse(null, null);    

using (HttpWebResponse httpResponse = (HttpWebResponse)request.EndGetResponse(result))
{
    using (Stream dataStream = httpResponse.GetResponseStream())
    {
        StreamReader reader = new StreamReader(dataStream);
        response = reader.ReadToEnd();
    }
}

Second Approach

string response = string.Empty;
AutoResetEvent waitHandle = null;
void GetResponse(string url)
{
    waitHandle = new AutoResetEvent(false);
    try
    {
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
        IAsyncResult asyncResult = request.BeginGetResponse(Callback, request);

        waitHandle.WaitOne();
    }
    catch { }
    finally
    {
        waitHandle.Close();
    }
}

    void Callback(IAsyncResult asyncResult)      
    {           

HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
            try
            {
                using (HttpWebResponse httpResponse = (HttpWebResponse)request.EndGetResponse(asyncResult))
                {
                    if (httpResponse.StatusCode == HttpStatusCode.OK)
                    {
                        using (Stream dataStream = httpResponse.GetResponseStream())
                        {
                            StreamReader reader = new StreamReader(dataStream);
                            response = reader.ReadToEnd();
                        }
                    }
                }
            }
            catch { }
            finally
            {
                waitHandle.Set();
            }
        }

解决方案

Why not execute the web request on the main thread? If you want the main thread to block, this is by far the easiest way to accomplish this.

这篇关于.NET线程 - HttpWebRequest的BeginGetResponse +的AutoResetEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆