title: C# Web - localhost:port works, 127.0.0.1:port doesn't work
body: <p>I just finished adding C# Web API components (Web API Models &amp; Controllers) to a <code>localhost</code> copy of an existing project.</p>
  
  <p>This Web API's GET-methods should be called from an Android app. In <a href="http://developer.android.com/tools/devices/emulator.html#networkaddresses" rel="noreferrer">this link</a> it's explained I should use <code>10.0.2.2</code> on the Android Emulator to get the computer's <code>127.0.0.1</code>.</p>
  
  <p>When I did this, it didn't work for my HttpRequest in the Android app. So I went to the Android browser and typed it directly, and it also didn't work.</p>
  
  <p>Then I tried using <code>127.0.0.1</code> instead of <code>localhost</code> in my computer's browser, and for some unknown reason it also doesn't work.. Is there a different between <code>localhost</code> and <code>127.0.0.1</code>? I always thought they were one and the same.</p>
  
  <p>Here is the <code>400 error</code> I get when using <code>127.0.0.1</code>:
  <img src="https://i.stack.imgur.com/TVqCq.png" alt="127.0.0.1 error 400 bad request"></p>
  
  <p>And with <code>localhost</code> everything works fine.</p>
  
  <p>So, my question: How can I use <code>localhost</code> on the Emulator (or, how can I fix the error I get when using <code>127.0.0.1</code> instead of <code>localhost</code>)? Also, I would like to know the difference between <code>localhost</code> and <code>127.0.0.1</code>, since I always thought they were the same.</p>
  
  <p>Thanks in advance for the responses.</p>
  
  <hr>
  
  <p>Edit 1:</p>
  
  <p>In <a href="https://stackoverflow.com/questions/752960/localhost-and-127-0-0-1-not-the-same">this stackoverflow question</a> they mention the host file in <code>System32</code> of Windows. I opened this file with Notepad++ (as Administrator) and uncommented the lines with <code>127.0.0.1   localhost</code> and <code>::1   localhost</code>. But unfortunally this didn't fixed the problem and I still can't use <code>127.0.0.1</code> on my computer as a replacement for <code>localhost</code>. Probably because my problem is the reversed (I can access <code>localhost</code>, but not <code>127.0.0.1</code>, instead of the other way around.)</p>
  
  <hr>
  
  <p>Edit 2:</p>
  
  <p>In <a href="https://stackoverflow.com/a/7382629/1682559">this stackoverflow answer</a> it's explained that the differences between <code>localhost</code> and <code>127.0.0.1</code> are:</p>
  
  <ul>
  <li><code>127.0.0.1</code> will be more easily recognized as an IP in some programming languages.</li>
  <li><code>localhost</code> can be changed to another IP in the computer's host-file (the file mentioned in my <em>Edit 1</em>).</li>
  <li>There are some differences between IPv4 and IPv6.</li>
  </ul>
  
  <p>All and all I kinda understand the differences now, I just don't get why my localhost is working, but 127.0.0.1 isn't..</p>
  
  <hr>
  
  <p>Edit 3:</p>
  
  <p>Does it have to do something with the <code>port</code> (I use <code>54408</code> as <code>port</code>)? I've opened <code>cmd</code> and did the following tests: </p>
  
  <ul>
  <li><code>ping localhost</code>: I'm getting a response with 4 times <code>Reply from ::1: time&lt;1ms</code>.</li>
  <li><code>ping 127.0.0.1</code>: I'm getting a response with 4 times <code>Reply from 127.0.0.1: bytes=32 time&lt;1ms TIL=128</code></li>
  <li><code>ping localhost:54408</code>: I'm getting an error <code>Ping request could not find localhost:54408. Please check the name and try again.</code></li>
  <li><code>ping 127.0.0.1:54408</code>: I'm getting an error <code>Ping request could not find 127.0.0.1:54408. Please check the name and try again.</code></li>
  </ul>
  
  <p>And like I've said before: <code>127.0.0.1:54408</code> in the browser gives the error seen in the image above. And <code>localhost:54408</code> works just fine..</p>
  
  <p>Still no one with an idea on how to fix this?</p>
  
  <hr>
  
  <p>Edit 4:</p>
  
  <p>Copy of my <code>hosts-file</code>, located in <code>C:\Windows\System32\drivers\etc</code>.</p>
  
  <pre><code># Copyright (c) 1993-2009 Microsoft Corp.
  #
  # This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
  #
  # This file contains the mappings of IP addresses to host names. Each
  # entry should be kept on an individual line. The IP address should
  # be placed in the first column followed by the corresponding host name.
  # The IP address and the host name should be separated by at least one
  # space.
  #
  # Additionally, comments (such as these) may be inserted on individual
  # lines or following the machine name denoted by a '#' symbol.
  #
  # For example:
  #
  #      102.54.94.97     rhino.acme.com          # source server
  #       38.25.63.10     x.acme.com              # x client host
  
  # localhost name resolution is handled within DNS itself.
  127.0.0.1   localhost
  ::1         localhost
  </code></pre>
  
  <p>The last two lines used to be:</p>
  
  <pre><code>#      127.0.0.1     localhost
  #      ::1           localhost
  </code></pre>
  
  <hr>
  
  <p>Edit 5 / Semi-Solution:</p>
  
  <p>After Jake C's suggestion I went looking for Visual Studio port configuration and found <a href="http://msdn.microsoft.com/en-us/library/vstudio/ms178109%28v=vs.100%29.aspx" rel="noreferrer">the following site</a>. At the section "<em>To specify a port for a Web application project that uses the Visual Studio Development Server</em>" I followed the instructions and changed the Web option in the Project Properties to <code>Use Visual Studio Development Server</code> with my <code>54408 port</code>.</p>
  
  <p>This was a great step in the right direction, since <code>127.0.0.1:54408</code> homepage now works. However, once I try to log in with the <code>Google OAuth</code> on the C# website, I was getting the following error:
  <img src="https://i.stack.imgur.com/2ryBF.png" alt="enter image description here"></p>
  
  <p>One of my ex-colleagues who worked on the C# web project told me once about <a href="https://stackoverflow.com/questions/11485271/google-oauth-2-authorization-error-redirect-uri-mismatch">this stackoverflow post</a>. In the answer of this post is stated that I should add the <code>redirect-urls</code> (in my case <code>127.0.0.1</code>) to the <code>Google APIs Console</code>.
  Right now I don't have access to this <code>Console</code> for my <code>localhost</code> project however, since it's obtained through <code>SVN</code>. I will ask to one of my supervisors for permission to view the <code>Google APIs Console</code> of this C# web project and perhaps edit the <code>redirect-urls</code> to include <code>127.0.0.1</code>.</p>
  
  <p>Once I've got it completely working I'll accept Jake C's answer, since his explanation of the <code>Http port configurations</code> did indeed help me find the answer.</p>
  
  <hr>
  
  <p>Edit 6:</p>
  
  <p>Ok, I've used my own Google APIs Console and created a new Project with a new Client ID. I've added both the <code>http://localhost:54408/Account/ExternalLoginCallback</code> and <code>http:127.0.0.1:54408/Account/ExternalLoginCallback</code> to the redirect-urls. Then in my C# web project's <code>App_Start/AuthConfig.cs</code> I've changed the Client settings to use this new client.</p>
  
  <p>Once again thanks Jake C for your suggestion to change the <code>http port configurations</code> in Visual Studio. This did the trick for me.</p>