title: Web API GetAsync not working on localhost
body: <p>I am learning how to connect to a ASP.NET Web API service using Visual Studio 2012 on my localhost.</p>
  
  <p>Here is the sample Web API Controller:</p>
  
  <pre><code>namespace ProductStore.Controllers
  {
  public class ProductsController : ApiController
  {
      static readonly IProductRepository repository = new ProductRepository();
  
      public IEnumerable&lt;Product&gt; GetAllProducts()
      {
          return repository.GetAll();
      }
  
      public Product GetProduct(int id)
      {
          Product item = repository.Get(id);
          if (item == null)
          {
              throw new HttpResponseException(HttpStatusCode.NotFound);
          }
          return item;
      }
  
      public IEnumerable&lt;Product&gt; GetProductsByCategory(string category)
      {
          return repository.GetAll().Where(
              p =&gt; string.Equals(p.Category, category, StringComparison.OrdinalIgnoreCase));
      }
  
      public HttpResponseMessage PostProduct(Product item)
      {
          item = repository.Add(item);
          var response = Request.CreateResponse&lt;Product&gt;(HttpStatusCode.Created, item);
  
          string uri = Url.Link("DefaultApi", new { id = item.Id });
          response.Headers.Location = new Uri(uri);
          return response;
      }
  
      public void PutProduct(int id, Product product)
      {
          product.Id = id;
          if (!repository.Update(product))
          {
              throw new HttpResponseException(HttpStatusCode.NotFound);
          }
      }
  
      public void DeleteProduct(int id)
      {
          repository.Remove(id);
      }
  }
  }
  </code></pre>
  
  <p>I am trying to connect to this Web API with the following code:</p>
  
  <pre><code>static async Task RunAsyncGet()
  {
      try
      {
          using (var client = new HttpClient())
          {
              client.BaseAddress = new Uri("http://localhost:9000/");
              client.DefaultRequestHeaders.Accept.Clear();
              client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  
              // HTTP GET
              HttpResponseMessage response = await client.GetAsync("/api/product/1");
              if (response.IsSuccessStatusCode)
              {
                  Product product = await response.Content.ReadAsAsync&lt;Product&gt;();
                  Console.WriteLine("{0}\t${1}\t{2}", product.Name, product.Price, product.Category);
              }
          }
      }
      catch (Exception ex)
      {         
          throw;
      }
  }
  </code></pre>
  
  <p>I have the following in the App.config (I found this online):</p>
  
  <pre><code>&lt;system.net&gt;
  &lt;defaultProxy enabled="false" useDefaultCredentials="false"&gt;
    &lt;proxy/&gt;
    &lt;bypasslist/&gt;
    &lt;module/&gt;
  &lt;/defaultProxy&gt;
  &lt;/system.net&gt;
  </code></pre>
  
  <p>When this line is executed, the application stops executing:</p>
  
  <pre><code>HttpResponseMessage response = await client.GetAsync("api/products/1");
  </code></pre>
  
  <p>What would be causing this?</p>
  
  <p>Thanks in advance</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here is the error:</p>
  
  <pre><code>System.Net.Http.HttpRequestException was caught   HResult=-2146233088  Message=An error occurred while sending the request.   Source=mscorlib StackTrace:
         at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
         at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
         at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
         at ProductStoreClientFormsApplication.Form1.&lt;RunAsyncGet&gt;d__0.MoveNext() in h:\Learning\WEB API\ProductStoreClientFormsApplication\ProductStoreClientFormsApplication\Form1.cs:line 33   InnerException: System.Net.WebException
         HResult=-2146233079
         Message=The underlying connection was closed: Unable to connect to the remote server.
         Source=System
         StackTrace:
              at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
              at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
         InnerException: System.Net.Sockets.SocketException
              HResult=-2147467259
              Message=An invalid argument was supplied
              Source=System
              ErrorCode=10022
              NativeErrorCode=10022
              StackTrace:
                   at System.Net.Sockets.Socket..ctor(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
                   at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress&amp; address, Socket&amp; abortSocket, Socket&amp; abortSocket6)
                   at System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback)
                   at System.Net.Connection.CompleteStartConnection(Boolean async, HttpWebRequest httpWebRequest)
              InnerException:
  </code></pre>