title: outbound web requests fail when made from IIS sites using shared IP address
body: <ul>
  <li>IIS 7.5 / Windows Server 2008 R2</li>
  <li>Multiple IIS sites bound to the same IP address, using host names. </li>
  <li>Inbound traffic to sites working fine.  </li>
  <li>Outbound web requests made by the back-end site code fail. Remote site returns 404 (NotFound). </li>
  <li>Verified via a network trace that the traffic is making it to the remove server.</li>
  <li>Same requests work fine if done from a site using a dedicated IP address (i.e. not shared w/ any other sites).</li>
  </ul>
  
  <p>Anyone have any ideas on how to make this work or what could be going wrong?</p>
  
  <p>Network trace on hosting server:</p>
  
  <p>Successful request from site w/ non-shared IP address:</p>
  
  <pre><code>No.     Time            Source                Destination           Protocol Info
     6366 15:54:35.590463 192.168.1.76          173.194.77.121        HTTP     GET /key/value/one/two HTTP/1.1 
     6369 15:54:35.599879 173.194.77.121        192.168.1.76          TCP      http &gt; 55407 [ACK] Seq=1 Ack=110 Win=344 Len=0
     6370 15:54:35.621587 173.194.77.121        192.168.1.76          HTTP     HTTP/1.1 200 OK  (application/json)
     6608 15:54:35.815774 192.168.1.76          173.194.77.121        TCP      55407 &gt; http [ACK] Seq=110 Ack=357 Win=509 Len=0
  </code></pre>
  
  <p>Failed request from site using a shared IP address:</p>
  
  <pre><code>No.     Time            Source                Destination           Protocol Info
     9720 15:54:39.244192 192.168.1.80          173.194.77.121        HTTP     GET /key/value/one/two HTTP/1.1 
     9760 15:54:39.256958 173.194.77.121        192.168.1.80          TCP      [TCP segment of a reassembled PDU]
     9761 15:54:39.256962 173.194.77.121        192.168.1.80          HTTP     HTTP/1.1 404 Not Found  (text/html)
     9762 15:54:39.257027 192.168.1.80          173.194.77.121        TCP      55438 &gt; http [ACK] Seq=212 Ack=1676 Win=512 Len=0
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>public static HttpWebRequest CreateWebRequest(string url, string method = "GET", string referer = null, string contentType = null, int timeout = 100000, string authentication = null, string bindToIpAddress = null, string host = null)
  {
      var request = (HttpWebRequest)WebRequest.Create(url);
  
      if (!string.IsNullOrWhiteSpace(bindToIpAddress))
      {
          IPAddress bindIp;
          if (!IPAddress.TryParse(bindToIpAddress, out bindIp))
          {
              throw new ArgumentException("bindToIpAddress");
          }
  
          request.ServicePoint.BindIPEndPointDelegate = ((sp, rep, rc) =&gt;
          {
              return new IPEndPoint(bindIp, 0);
          });
      }
  
      request.Accept = "*/*";
      request.ContentType = contentType;
      request.Referer = referer;
      request.Method = method;
      request.Timeout = timeout;
  
      if (!string.IsNullOrWhiteSpace(host))
      {
          request.Host = host;
      }
  
      return request;
  }
  
  string GetData()
  {
      try
      {
          string result;
  
          var request = CreateWebRequest("http://jsonplaceholder.typicode.com/posts/1", 
                                         "GET", 
                                         "somedomain.com", 
                                         timeout: (10 * 1000), 
                                         bindToIpAddress: "192.168.27.133" /*site IP*/);
  
          request.Accept = "application/json";
  
          using (var response = request.GetResponse())
          {
              using (var sr = new StreamReader(response.GetResponseStream()))
              {
                  result = sr.ReadToEnd();
              }
          }
  
          return result;
      }
      catch (Exception ex)
      {
          return null;
      }
  }
  </code></pre>