执行异步Web请求时出现问题

时间:2010-03-26 18:33:50

标签: c# asynchronous windows-phone-7

有谁能告诉我这个简单的代码我做错了什么? 当我运行它时,它挂起

using (Stream postStream = request.EndGetRequestStream(asynchronousResult))

如果我注释掉requestState.Wait.WaitOne();代码执行正确,但显然不等待响应。我猜测对EndGetRequestStream的调用是以某种方式将我返回到主线程的上下文?我很确定我的代码与样本基本相同(MSDN Documentation

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Text;

namespace SBRemoteClient
{
    public class JSONClient
    {

        public string ExecuteJSONQuery(string url, string query)
        {
            System.Uri uri = new Uri(url);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = "POST";
            request.Accept = "application/json";
            byte[] requestBytes = Encoding.UTF8.GetBytes(query);
            RequestState requestState = new RequestState(request, requestBytes);
            IAsyncResult resultRequest = request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), requestState);
            requestState.Wait.WaitOne();
            IAsyncResult resultResponse = (IAsyncResult)request.BeginGetResponse(new AsyncCallback(GetResponseStreamCallback), requestState);
            requestState.Wait.WaitOne();

            return requestState.Response;
        }

        private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
        {
            try
            {
                RequestState requestState = (RequestState)asynchronousResult.AsyncState;
                HttpWebRequest request = requestState.Request;
                using (Stream postStream = request.EndGetRequestStream(asynchronousResult))
                {
                    postStream.Write(requestState.RequestBytes, 0, requestState.RequestBytes.Length);
                }
                requestState.Wait.Set();
            }
            catch (Exception e) {
                Console.Out.WriteLine(e);
            }
        }

        private static void GetResponseStreamCallback(IAsyncResult asynchronousResult)
        {
            RequestState requestState = (RequestState)asynchronousResult.AsyncState;
            HttpWebRequest request = requestState.Request;
            using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult))
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader streamRead = new StreamReader(responseStream))
                    {
                        requestState.Response = streamRead.ReadToEnd();
                        requestState.Wait.Set();
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

几件事:

  1. 我认为您不能重复使用相同的请求对象。即每个HttpWebRequest实例都可以调用一次BeginGetRequestStream。 如果你想执行两个请求(调用服务器),每个请求需要两个HttpWebRequest实例。

  2. 如果您想要同步行为,您有两个选择:使用GetRespose方法 或以同步方式使用开始/结束。为此,您不需要将回调传递给BeginGetRequestStream方法(您可以传递null)。 从BeginGetRequestStream(IAsyncResult)获取返回值并将其传递给EndGetRequestStream方法:

    AsyncResult resultRequest = request.BeginGetRequestStream(null, null);
    Stream postStream = request.EndGetRequestStream(asynchronousResult)
    
  3. EndGetRequestStream将阻塞,直到请求完成为止(如果你是从UI那里做的那么这很糟糕,但它仍然可以工作)。