title: 'System.Net.HttpWebRequest' does not contain a definition for 'GetResponse'
body: <p>I am creating a method for consuming a restful GET method in Visual studio 2013, with framework 4.5. This is windows_phone_8 (targeting Phone OS 8.0) application. Here is my code.</p>
  
  <pre><code>static string HttpGet(string url)
      {
          HttpWebRequest req = WebRequest.Create(url)
                               as HttpWebRequest;
          string result = null;
          using (HttpWebResponse resp = req.GetResponse()
                                        as HttpWebResponse)
          {
              StreamReader reader =
                  new StreamReader(resp.GetResponseStream());
              result = reader.ReadToEnd();
          }
          return result;
      }
  </code></pre>
  
  <p>But I am getting a build error as follows </p>
  
  <blockquote>
    <p>'System.Net.HttpWebRequest' does not contain a definition for
    'GetResponse' and no extension method 'GetResponse' accepting a first
    argument of type 'System.Net.HttpWebRequest' could be found (are you
    missing a using directive or an assembly reference?)</p>
  </blockquote>
  
  <p>I don't know why it happens, the same code is working fine with Windows_application in the same environment.</p>
  
  <p><strong>Update :</strong> I have tried with the web client method also</p>
  
  <pre><code> WebClient client = new WebClient();
  
              client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
  
              Stream data = client.OpenRead("http://192.168.10.73:8087/cisms/mobilews/login/userNameCheck?userName=supervisor");
              StreamReader reader = new StreamReader(data);
              string s = reader.ReadToEnd();
              data.Close();
              reader.Close();
  </code></pre>
  
  <p>Got another set of errors ...</p>
  
  <blockquote>
    <p>Error 1   'System.Net.WebHeaderCollection' does not contain a definition
    for 'Add' and no extension method 'Add' accepting a first argument of
    type 'System.Net.WebHeaderCollection' could be found (are you missing
    a using directive or an assembly reference?)</p>
    
    <p>Error 2   'System.Net.WebClient' does not contain a definition for
    'OpenRead' and no extension method 'OpenRead' accepting a first
    argument of type 'System.Net.WebClient' could be found (are you
    missing a using directive or an assembly reference?)</p>
    
    <p>Error 3   'System.Net.HttpWebRequest' does not contain a definition for
    'GetResponse' and no extension method 'GetResponse' accepting a first
    argument of type 'System.Net.HttpWebRequest' could be found (are you
    missing a using directive or an assembly reference?)</p>
  </blockquote>
  
  <p><strong>Update 2 :</strong> </p>
  
  <p>I have changed the code as follows, based on @Gavin' answer..  </p>
  
  <pre><code>static async void HttpGet(string url)
      {
          Uri uri = new Uri(url);
          string result = null;
          HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
          request.Method = "GET";
          using (var response = (HttpWebResponse)(await Task&lt;WebResponse&gt;.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null)))
          {
              StreamReader reader = new StreamReader(response.GetResponseStream());
              result = reader.ReadToEnd();
          }
      }
  </code></pre>
  
  <p>But the control goes back to the calling event, from the following line </p>
  
  <pre><code>using (var response = (HttpWebResponse)(await Task&lt;WebResponse&gt;.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null)))
  </code></pre>
  
  <p>any help on this would be greatly appreciated.</p>
  
  <p><strong>Answer :</strong> </p>
  
  <p>I changed the code as follows and it is working now..</p>
  
  <pre><code>public async Task&lt;string&gt; httpRequest(string url)
          {
              Uri uri = new Uri(url);
              HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
              string received;
  
              using (var response = (HttpWebResponse)(await Task&lt;WebResponse&gt;.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null)))
              {
                  using (var responseStream = response.GetResponseStream())
                  {
                      using (var sr = new StreamReader(responseStream))
                      {
  
                          received = await sr.ReadToEndAsync();
                      }
                  }
              }
  
              return received;
          }
  </code></pre>
  
  <p>the calling part is as follows...</p>
  
  <pre><code>private async void Button_Click(object sender, RoutedEventArgs e)
          {
              string uriString = "http://192.168.10.73:8087/cisms/mobilews/login/userNameCheck?userName=supervisor";
              var response = await httpRequest(uriString);
          }
  </code></pre>
  
  <p><strong>Update 3:</strong></p>
  
  <p>I have one more issue in processing POST request. The code I have tried is given below.</p>
  
  <pre><code>static string HttpPost(string url, string[] paramName, string[] paramVal)
          {
              HttpWebRequest req = WebRequest.Create(new Uri(url))
                                   as HttpWebRequest;
              req.Method = "POST";
              req.ContentType = "application/x-www-form-urlencoded";
  
              // Build a string with all the params, properly encoded.
              // We assume that the arrays paramName and paramVal are
              // of equal length:
              StringBuilder paramz = new StringBuilder();
              for (int i = 0; i &lt; paramName.Length; i++)
              {
                  paramz.Append(paramName[i]);
                  paramz.Append("=");
                  paramz.Append(HttpUtility.UrlEncode(paramVal[i]));
                  paramz.Append("&amp;");
              }
  
              // Encode the parameters as form data:
              byte[] formData =
                  UTF8Encoding.UTF8.GetBytes(paramz.ToString());
              req.ContentLength = formData.Length;
  
              // Send the request:
              using (Stream post = req.GetRequestStream())
              {
                  post.Write(formData, 0, formData.Length);
              }
  
              // Pick up the response:
              string result = null;
              using (HttpWebResponse resp = req.GetResponse()
                                            as HttpWebResponse)
              {
                  StreamReader reader =
                      new StreamReader(resp.GetResponseStream());
                  result = reader.ReadToEnd();
              }
  
              return result;
          }
  </code></pre>
  
  <p>This method is having two build errors in Windows phone 8 application</p>
  
  <blockquote>
    <p>Error 1   'System.Net.HttpWebRequest' does not contain a definition for
    'GetRequestStream' and no extension method 'GetRequestStream'
    accepting a first argument of type 'System.Net.HttpWebRequest' could
    be found (are you missing a using directive or an assembly reference?)</p>
    
    <p>Error 2   'System.Net.HttpWebRequest' does not contain a definition for
    'GetResponse' and no extension method 'GetResponse' accepting a first
    argument of type 'System.Net.HttpWebRequest' could be found (are you
    missing a using directive or an assembly reference?)</p>
  </blockquote>
  
  <p>Thanks
  Sebastian</p>