Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebClient and multiple network adaptors

Tags:

c#

webclient

I am using WebClient to try and get a string response from a piece of hardware connected locally to my PC. My PC has a network adaptor connected to a LAN and a second adaptor that is connected only to my piece of hardware.

If I use IE with the URL: http://169.254.103.127/set.cmd?user=admin+pass=12345678+cmd=getpower I get back a string in response. I am trying to use the following code snippet to acheive the same thing:

using (WebClient client = new WebClient())
        {
            client.Proxy = WebRequest.DefaultWebProxy;
            client.Credentials = CredentialCache.DefaultCredentials;
            client.Proxy.Credentials = CredentialCache.DefaultCredentials;

            client.Headers["User-Agent"] =
            "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0) " +
            "(compatible; MSIE 6.0; Windows NT 5.1; " +
            ".NET CLR 1.1.4322; .NET CLR 2.0.50727)";

            String test = client.DownloadString(@"http://169.254.103.127/set.cmd?user=admin+pass=12345678+cmd=getpower");
        }

This snippet works if I disable my network adaptor that is connected to my LAN, but times out otherwise.

Can someone explain why this is happening and what I need to do to route the request to the correct network?

like image 920
JNH Avatar asked Oct 18 '22 22:10

JNH


2 Answers

Your issue relies on default metric used by your network adaptors.

Short answer

Got to your local adaptor setting (the one which is connected to your device)-> Properties-> IP v4 Properties -> Advanced -> Uncheck Automatic metric -> Put a default value like 900.

Explanation

Metric is what your OS use to choose the best way for a certain destination (Mostly when you have more than 1 route available for the same destination)

Your LAN adaptor have probably a default gateway on which the metric is defined automaticaly by your OS. As this gateway was defined manually or by DHCP, by default, Windows will use a better metric on this adaptor than the other adaptor with automatic link-local address.

This means that your OS will choose in priority the default gateway (LAN) to go to 169.254.103.127 instead of your local card

You can use this command to check your route and your metric:

   route print 

For example in my computer with the default configuration :

          0.0.0.0          0.0.0.0      192.168.1.1    192.168.1.100    276  // Default gateway
        127.0.0.0        255.0.0.0         On-link         127.0.0.1    306
        127.0.0.1  255.255.255.255         On-link         127.0.0.1    306
  127.255.255.255  255.255.255.255         On-link         127.0.0.1    306
      169.254.0.0      255.255.0.0         On-link    169.254.239.60    261 // Where you want to go
   169.254.239.60  255.255.255.255         On-link    169.254.239.60    261
  169.254.255.255  255.255.255.255         On-link    169.254.239.60    261
      192.168.1.0    255.255.255.0         On-link     192.168.1.100    276
    192.168.1.100  255.255.255.255         On-link     192.168.1.100    276
    192.168.1.255  255.255.255.255         On-link     192.168.1.100    276

As 276 > 261, a ping to 169.254.103.127 will take the lan 192.168.1.100 instead of 169.254.239.60

like image 185
Perfect28 Avatar answered Oct 21 '22 16:10

Perfect28


Setting the proxy to null solved my problem, this is the finished function:

private String SendCommand(String command)
    {
        String response = null;

        using (WebClient client = new WebClient())
        {
            client.Proxy = null;
            response = client.DownloadString(command);
        }

        return response;
    }
like image 22
JNH Avatar answered Oct 21 '22 17:10

JNH