title: WebRequest BeginGetResponse/EndGetResponse throws exception when using Task.FromAsync
body: <p>I try to asyncify the POST operation to a REST web service using the WebRequest.</p>
  
  <p>So from what I know already, I need to use the <code>Task.Factory.FromAsync&lt;T&gt;(BeginXyz, EndXyz, null)</code>, seems like its working for the <code>GetRequestStream</code> pair methods, since I get no exception and the byte[] is filled with values.</p>
  
  <pre><code>Stream requestStream = await Task.Factory.FromAsync&lt;Stream&gt;(
                          request.BeginGetRequestStream, request.EndGetRequestStream, null);
  byte[] postBytes = Encoding.UTF8.GetBytes(requestData);
  await requestStream.WriteAsync(postBytes, 0, postBytes.Length);
  </code></pre>
  
  <p>Fine, the above code is working, and the next part throwing the exception.</p>
  
  <pre><code>WebResponse webResponse = await Task.Factory.FromAsync&lt;WebResponse&gt;(
                          request.BeginGetResponse, request.EndGetResponse, null);
  </code></pre>
  
  <p>The exception I get to every request.</p>
  
  <blockquote>
    <p>System.NotSupportedException: Specified method is not supported.</p>
    
    <p>StackTrace:</p>
    
    <blockquote>
      <p>at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback
      beginMethod, Object state)    at
      System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult
      asyncResult)    at System.Func`2.Invoke(T arg)    at
      System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult
      iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean
      requiresSynchronization)
      --- End of stack trace from previous location where exception was thrown ---    at
      System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
      task)    at
      System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
      task)    at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 
      at
      Gtas.Core.ServiceRepository.d__8.MoveNext()</p>
    </blockquote>
    
    <p>InnerException StackTrace:   at
    System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult
    asyncResult) at
    System.Net.Browser.ClientHttpWebRequest.&lt;>c__DisplayClasse.b__d(Object
    sendState) at
    System.Net.Browser.AsyncHelper.&lt;>c__DisplayClass1.b__0(Object
    sendState)</p>
  </blockquote>
  
  <p>A full code to execute the request should be like that:</p>
  
  <pre><code>Stream requestStream = await Task.Factory.FromAsync&lt;Stream&gt;(
                          request.BeginGetRequestStream, request.EndGetRequestStream, null);
  byte[] postBytes = Encoding.UTF8.GetBytes(requestData);
  await requestStream.WriteAsync(postBytes, 0, postBytes.Length);
  
  
  WebResponse webResponse = await Task.Factory.FromAsync&lt;WebResponse&gt;(
                          request.BeginGetResponse, request.EndGetResponse, null);
  Stream streamResponse = webResponse.GetResponseStream();
  StreamReader streamRead = new StreamReader(streamResponse);
  string responseString = await streamRead.ReadToEndAsync();
  // ... Do something with the result data
  </code></pre>
  
  <p>I don't quite understand what the error is.</p>
  
  <p>I tried several implementation from stackoverflow and some MSDN posts. Nothing seems to working.</p>
  
  <p>Using callbacks working fine. But it's good to know why I can't make it work.</p>
  
  <p>Any thoughts how to accomplish this?</p>