异步使用HttpWebRequest而不阻塞UI线程

时间:2013-02-26 20:52:57

标签: c# winforms asynchronous httpwebrequest httpwebresponse

我试图在WinForms应用程序中异步使用HttpWebRequestHttpWebResonse而不阻止我的UI线程。

我看到这个similar SO Question解释了如何做到这一点。但是,我并非100%确定接受的答案是正确的方法。接受的答案是使用BeginGetResponse

根据MSDN documentation

  

BeginGetResponse方法需要一些同步设置任务   完成(DNS解析,代理检测和TCP套接字连接,   例如)此方法变为异步之前。结果是,   永远不应该在用户界面(UI)线程上调用此方法   因为它可能需要一些时间,通常是几秒钟。

有人可以请我提供正确的方法。这是我的C#函数,我正在尝试“正常”工作而不阻塞我的UI线程:

    private IDictionary<string, object> CreateWebRequest(string endPoint, string method, string data, bool addAuthHeader)
    {
        var req = (HttpWebRequest)WebRequest.Create(endPoint);
        req.Method = method;
        if (addAuthHeader)
        {
            req.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + _accessToken.AccessToken);
        }

        if (!string.IsNullOrEmpty(data))
        {
            var utfenc = new UTF8Encoding();
            byte[] buffer = utfenc.GetBytes(data);
            req.ContentLength = buffer.Length;
            req.ContentType = "application/x-www-form-urlencoded";

            using (Stream strm = req.GetRequestStream())
            {
                strm.Write(buffer, 0, buffer.Length);
                strm.Close();
            }
        }

        HttpWebResponse response = null;
        try
        {
            response = (HttpWebResponse)req.GetResponse();
        }
        catch (WebException e)
        {
            if (_accessToken == null)
            {
                throw;
            }
            var responseError = (HttpWebResponse)e.Response;
            if (responseError.StatusCode == HttpStatusCode.Unauthorized)
            {
                var stackTrace = new StackTrace();
                var q = stackTrace.GetFrame(1).GetMethod().Name;
                RefreshAccessCode();

                req = (HttpWebRequest)WebRequest.Create(endPoint);
                req.Method = method;
                if (addAuthHeader)
                {
                    req.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + _accessToken.AccessToken);
                }
                response = (HttpWebResponse)req.GetResponse();
            }
        }

        string jsonResponse = null;

        if (response != null)
            using (Stream stream = response.GetResponseStream())
            {
                if (stream != null)
                {
                    using (var reader = new StreamReader(stream))
                    {
                        jsonResponse = reader.ReadToEnd();
                    }
                }
            }

        return DeserializeResponse(jsonResponse);
    }

2 个答案:

答案 0 :(得分:3)

如果你对除WebRequest之外的东西开放,我就是System.Net.WebClient的粉丝。它使用System.Threading.Tasks而不是BeginGetResponse()和EndGetResonse()。

public async Task<Dictionary<string, object>> CreateWebRequest(string endPoint, string method, string data, bool addAuthHeader)
{
    WebClient client = new WebClient();

    if (addAuthHeader)
    {
        client.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + _accessToken.AccessToken);
    }

    byte[] buffer = null;

    if (!string.IsNullOrEmpty(data))
    {
        var utfenc = new UTF8Encoding();
        buffer = utfenc.GetBytes(data);
    }
    else
    {
        buffer = new byte[0];
    }

    return await client.UploadDataTaskAsync(endPoint, method, buffer)
        .ContinueWith((bytes) =>
            {
                string jsonResponse = null;

                using (var reader = new StreamReader(new MemoryStream(bytes)))
                {
                    jsonResponse = reader.ReadToEnd();
                }

                return DeserializeResponse(jsonResponse);
            });
    }
}

答案 1 :(得分:0)

要使用现有代码,只需添加要在UI线程上执行的方法,并在单独的线程上创建Web请求。 Web请求完成后,在UI线程上调用final方法。所以,一个简单的版本将是:

//do some stuff that wont block the ui thread here
ThreadPool.QueueUserWorkItem((o) => 
{
    IDictionary<string, object> result = 
        CreateWebRequest("lalala", "lol", "butts", true);
    BeginInvoke(OnAsyncWebRequestComplete, result);
}, null);

private void OnAsyncWebRequestComplete(IDictionary<string, object> result)
{
    //do some stuff on the UI thread here
}