3

我正在尝试在 WinForms 应用程序中异步使用HttpWebRequest而不HttpWebResonse阻塞我的 UI 线程。

我看到了这个类似的 SO Question解释了如何去做。但是,我不能 100% 确定接受的答案是正确的方法。接受的答案是使用BeginGetResponse.

根据MSDN 文档

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

有人可以为我提供正确的方法来做到这一点。这是我试图在不阻塞 UI 线程的情况下“正确”工作的 C# 函数:

    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);
    }
4

2 回答 2

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);
            });
    }
}
于 2013-02-26T21:25:27.360 回答
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
}
于 2013-02-26T21:11:15.877 回答