Problem Executing Async Web Request
        Posted  
        
            by davidhayes
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by davidhayes
        
        
        
        Published on 2010-03-26T18:33:49Z
        Indexed on 
            2010/03/28
            20:03 UTC
        
        
        Read the original article
        Hit count: 319
        
Hi
Can anyone tell me what I've done wrong with this simple code?
When I run it it hangs on
using (Stream postStream = request.EndGetRequestStream(asynchronousResult))
If I comment out the requestState.Wait.WaitOne(); line the code executes correctly but obviously doesn't wait for the response. I'm guessing the the call to EndGetRequestStream is somehow returning me to the context of the main thread?? I'm pretty sure my code is essentially the same as the sample though (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();
                    }
                }
            }
        }
    }
}
© Stack Overflow or respective owner