WebRequest BeginGetRequestStream和BeginGetResponse不会触发它们的回调

时间:2011-07-04 19:05:32

标签: c# silverlight web-services httpwebrequest

我有一个使用JSON的PHP Web服务,我需要从Silverlight应用程序发布请求。我以前在常规的C#应用​​程序中使用过HttpWebRequest类,它运行得很完美。但是,在我的Silverlight应用程序中,BeginGetRequestStreamBeginGetResponse方法的回调未被调用。

我试图在各方面都进行测试而没有成功。请注意,我发送的数据不长 - 大多数时候只是一个32个字符的字符串。我检查了回调是否被调用,并且它们没有被调用。这是我的代码:

    public void Request(Command cmd, Dictionary<string, string> data)
            {
                Dictionary<string, object> fullRequest = new Dictionary<string,object>();
                HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";                 

                StringWriter writer = new StringWriter(new StringBuilder());
                JsonSerializer serializer = new JsonSerializer();
                serializer.Serialize(writer, fullRequest);
                string query = "query=" + writer.ToString();

                request.ContentLength = query.Length;
                request.ContentType = "text/json";
                _queue.Enqueue(new KeyValuePair<Command, string>(cmd, query));
                request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);

public void GetRequestStreamCallback(IAsyncResult target)
        {

            HttpWebRequest request = (HttpWebRequest)target.AsyncState;

            Stream postStream = request.EndGetRequestStream(target);
            string postData = "";
            //not sure why I have to do this. in the Silverlight documentation
            //Queue<> is supposed to have the SyncRoot property
            //but  I get an error if I just try _queue.SyncRoot
            lock ((_queue as ICollection).SyncRoot)
            {
                postData = _queue.Dequeue().Value;

            }
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            postStream.Write(byteArray, 0, postData.Length);
            postStream.Close();
            request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
        }


 public void GetResponseCallback(IAsyncResult result)
        {

            HttpWebRequest request = (HttpWebRequest)result.AsyncState;            
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
            Stream streamResponse = response.GetResponseStream();
            StreamReader streamRead = new StreamReader(streamResponse);
            string responseString = streamRead.ReadToEnd();            
            streamResponse.Close();
            streamRead.Close();            
            response.Close();           
            JsonSerializer serializer = new JsonSerializer();
            Dictionary<string, object> responseData = new Dictionary<string, object>();
            responseData = (Dictionary<string, object>)serializer.Deserialize(new StringReader(responseString), typeof(Dictionary<string, object>));
            if (_data.ContainsKey(responseData["key"] as string))
            {
                _data[responseData["key"] as string] = responseData;
            }
            else
            {
                _data.Add(responseData["key"] as string, responseData);
            }

我也试过这个:

IAsyncResult resultRequest = request.BeginGetRequestStream(null, null) as IAsyncResult;
Stream postStream = request.EndGetRequestStream(resultRequest );

要同步获取流(是的,糟糕的风格,我只是想看看它是否可行)。没有成功。 提前致谢。

0 个答案:

没有答案