Identify the network adapter receiving the HTTP response

I have several network adapters on the desktop. I communicate with one server using HTTP. When I send and receive a reply. I want to know which NIC gets used to communicating for a specific HTTP request.

I need to know this because several of the network adapters can be connected to the private network, and only one network adapter is connected to the desired HTTP server for which I want to host the callback URL. I am using C # ,. Net 4.0

Any help would be appreciated.

+4
source share
3 answers

You cannot ask from which network adapter the request comes from, because you do not care. But you can get the IP address that the request came to:

var localAddress = Request.ServerVariables["LOCAL_ADDR"]; 

More about server variables here .

+3
source

As Dave Van den Aind says, plus it gets the IP addresses of the adapters on the machine: -

  foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces()) { Console.WriteLine("Name: " + netInterface.Name); Console.WriteLine("Description: " + netInterface.Description); Console.WriteLine("Addresses: "); IPInterfaceProperties ipProps = netInterface.GetIPProperties(); foreach (UnicastIPAddressInformation addr in ipProps.UnicastAddresses) { Console.WriteLine(" " + addr.Address.ToString()); } Console.WriteLine(""); } 
+1
source

.NET HTTPWebRequest and related objects do not drop LocalEndpoint information onto the stack for you. If you use something like FiddlerCore, you can try the request on each interface, or you can use this method: http://blogs.msdn.com/b/fiddler/archive/2011/03/09/mapping-socket- or-port-to-owner-process-in-c-sharp-dotnet.aspx to try that the local adapter was used to connect to the process.

But overall, this approach will be fragile, and you should carefully consider whether you really need this feature.

+1
source

All Articles