title: Need to make synchronous REST calls in WP8 using HttpWebRequest
body: <p>I've looked at some of the answers for similar questions and can't seem to find something that is applicable to what I'm doing.  I need to make a few synchronous requests using HttpWebRequest (some using each verb, GET/PUT/POST/DELETE) and can't seem to get it to work.  The example below works great when I manually use a 'refresh' button that I have in the design (works for any verb specified), but when I uncomment the section in 'b_send_Click' it doesn't work.  What I'm looking for is a wrapper method that will encapsulate the REST client (the way 'b_send_Click' does in this example) and then take some action when the call is complete (the commented section in 'b_send_Click' as an example).  Any ideas?  By the way, this works well as a wrapper for async REST calls but I can't get the sync working...</p>
  
  <pre><code>using Microsoft.Phone.Controls;
  using System;
  using System.IO;
  using System.Net;
  using System.Text;
  using System.Windows;
  
  namespace WP8Rest
  {
      public partial class MainPage : PhoneApplicationPage
      {
          // global variables
          public static string url = "http://mywebsite.com/API/some_api";
          public static string request_body = "";
          public static HttpWebRequest client = null;
          public static HttpWebResponse response = null;
          public static string server_response = "";
          public static bool request_done = false;
  
          public MainPage()
          {
              InitializeComponent();
          }
  
          private void b_send_Click(object sender, RoutedEventArgs e)
          {
              rest_request(sender, e);
  
              /*
              while (!request_done)
              {
                  Thread.Sleep(100);
              }
  
              if (response != null)
              {
                  l_status_code.Text = response.StatusCode.ToString();
                  l_status_description.Text = response.StatusDescription.ToString();
                  l_response.Text = server_response;
              }
              else
              {
                  l_status_code.Text = "0";
                  l_status_description.Text = "Unable to complete request...";
                  l_response.Text = "Unable to complete request...";
              }
               */
          }
  
          private void rest_request(object sender, RoutedEventArgs e)
          {
              request_done = false;
              server_response = "";
              request_body = tb_reqbody.Text;
              client = (HttpWebRequest)WebRequest.Create(url);
  
              client.Method = tb_verb.Text;
              client.AllowAutoRedirect = true;
  
              switch (tb_verb.Text)
              {
                  case "GET":
                      client.BeginGetResponse(new AsyncCallback(GetResponseCallback), client);
                      break;
  
                  case "PUT":
                      client.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), client);
                      client.ContentType = "application/json";
                      break;
  
                  case "POST":
                      client.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), client);
                      client.ContentType = "application/json";
                      break;
  
                  case "DELETE":
                      client.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), client);
                      client.ContentType = "application/json";
                      break;
  
                  default:
                      MessageBox.Show("Use GET, PUT, POST, or DELETE.");
                      return;
              }
  
              l_response.Text = "Request sent...";
              return;
          }
  
          private static void GetRequestStreamCallback(IAsyncResult async_result)
          {
              HttpWebRequest request = (HttpWebRequest)async_result.AsyncState;
              Stream request_body_stream = request.EndGetRequestStream(async_result);
              byte[] request_body_bytearray = Encoding.UTF8.GetBytes(request_body);
              request_body_stream.Write(request_body_bytearray, 0, request_body.Length);
              request_body_stream.Close();
              request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
          }
  
          private static void GetResponseCallback(IAsyncResult async_result)
          {
              HttpWebRequest request = (HttpWebRequest)async_result.AsyncState;
              response = (HttpWebResponse)client.EndGetResponse(async_result);
              Stream response_body_stream = response.GetResponseStream();
              StreamReader stream_reader = new StreamReader(response_body_stream);
              server_response = stream_reader.ReadToEnd();            
              response_body_stream .Close();
              stream_reader.Close();
              response.Close();
              request_done = true;
          }
  
          private void b_refresh_Click(object sender, RoutedEventArgs e)
          {
              if (response != null)
              {
                  l_response.Text = server_response;
                  l_status_code.Text = response.StatusCode.ToString();
                  l_status_description.Text = response.StatusDescription.ToString();
              }
              else
              {
                  l_response.Text = "No response...";
              }
          }
      }
  }
  </code></pre>
  
  <p>Per @Industry86 I was able to get Microsoft.Net.Http installed.  Changed code to:</p>
  
  <pre><code>private async void b_send_Click(object sender, RoutedEventArgs e)
  {
      l_response.Text = myMethod();
  }
  
  async Task&lt;string&gt; myMethod()
  {
      string address = "http://dev.getcube.com:65533/rest.svc/API/mirror";
      HttpClient client = new HttpClient();
      HttpResponseMessage response = await client.GetAsync(address);
      string responseText = await response.Content.ReadAsStringAsync();
      return responseText;
  }
  </code></pre>
  
  <p>Problem now is that it won't compile - "Cannot implicitly convert type 'System.Threading.Tasks.Task' to 'string'.  I changed the line in b_send_Click to <code>l_response.Text = (myMethod()).Result;</code> (not sure if this is correct or not) and when I click the 'b_send' button, it turns orange and the server never sees the request.</p>