开发者

Identify NIC on which HTTP response come

I have multiple NIC on my desktop. I am communication to one开发者_如何学Python server using HTTP. When I send and receive response. I want to know out of all which NIC is getting used for communicating for a particular HTTP request.

I need to know this as multiple some of NIC can be connected to private network and only one NIC is connected to desired HTTP server for which I want to host Callback URL. I am using C#, .Net 4.0

Any help will be appreciated.


You can't really ask from which NIC the request comes from, because you shouldn't care. But you can get the IP on which the request came in:

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

You can find more about server variables here.


As Dave Van den Eynde says plus this 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("");
  }


.NET's HTTPWebRequest and related objects do not plumb the LocalEndpoint information up the stack for you. If you use something like FiddlerCore, you could try the request on each interface, or you could use a technique like this: http://blogs.msdn.com/b/fiddler/archive/2011/03/09/mapping-socket-or-port-to-owner-process-in-c-sharp-dotnet.aspx to attempt to infer which local adapter was used for an in-progress connection.

But generally, this approach is going to be fragile, and you should consider carefully whether you really need this feature.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜