title: Best way to specify the local endpoint for HTTP request?
body: <p>I am trying to specify the local endpoint for some HTTP requests that I need to make.</p>
  
  <p>Here is a simplified version of what I am doing now:</p>
  
  <pre><code>using (HttpClient client = new HttpClient())
    using (HttpResponseMessage response = await client.SendAsync(request))
      if (response.IsSuccessStatusCode)
        result = await response.Content.ReadAsStringAsync();
  </code></pre>
  
  <p>I am not tied to <code>System.Net.Http.HttpClient</code>, and I am totally open to using other classes, like <code>System.Net.HttpWebRequest</code>, to get the job done.</p>
  
  <p>It would appear at least theoretically possible since Windows and <code>System.Net.Sockets</code> allows this with the <code>Bind</code> method. <a href="http://blogs.technet.com/b/networking/archive/2009/04/24/source-ip-address-selection-on-a-multi-homed-windows-computer.aspx" rel="nofollow noreferrer">Source IP address selection on a Multi-Homed Windows Computer</a>, <a href="http://msdn.microsoft.com/en-us/library/system.net.sockets.socket(v=vs.110).aspx" rel="nofollow noreferrer">MSDN - Socket Class (System.Net.Sockets)</a></p>
  
  <p>Obviously this isn't possible, but in an ideal world, I could somehow access the underlying Socket like this:</p>
  
  <pre><code>client.Socket.Bind(new IPEndPoint(167772161, 80));
  </code></pre>
  
  <p>Is there any way to do this without writing an entire HTTP client?</p>
  
  <hr>
  
  <p>Here is a little more background on why I'm trying to do this, in case someone has a different solution all together. We have a webfarm with several machines each using a single NIC with several IP addresses (all on the same gateway). We'd like to have each application using it's own IP address for external requests.</p>
  
  <p>Here is a simplified version:</p>
  
  <ol>
  <li>Machine 1
  
  <ol>
  <li>App 1 (10.0.1.10 / 255.255.0.0)</li>
  <li>App 2 (10.0.1.11 / 255.255.0.0)</li>
  <li>etc..</li>
  </ol></li>
  <li>Machine 2  
  
  <ol>
  <li>App 1 (10.0.2.10 / 255.255.0.0)</li>
  <li>App 2 (10.0.2.11 / 255.255.0.0)</li>
  <li>etc...</li>
  </ol></li>
  <li>etc...</li>
  </ol>
  
  <p>Traffic is routed from 172.16.0.2 (app 1 external) to 10.0.x.10 (app 1 internal), from 172.16.0.3 (app 2 external) to 10.0.x.11 (app 2 internal), etc. We are trying to avoid external requests from app 2 using 172.16.0.2 (app 1 external), because it's using 10.0.x.10 as a local address instead of 10.0.x.11.</p>
  
  <p>Quick network diagram of the simplified example, in case that helps at all:
  <img src="https://i.stack.imgur.com/00l4P.png" alt="example network diagram"></p>