3

I am working on uwp app. I edited hosts file and redirecting www.example.com to local server IP(192.168.1.187). In browser website is loading properly but in application

System.Net.Dns.GetHostAddresses("https://www.exaple.com")[0]

Throws System.Net.Sockets.SocketException

If I remove http/https, it is working

System.Net.Dns.GetHostAddresses("www.exaple.com")[0]
output => 192.168.1.187

I want to specifically make a request to https://www.example.com.

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
Shashi kumar S
  • 873
  • 3
  • 10
  • 21
  • If you want to make a request - then make a request, not resolve hostname to IP address. – Evk May 17 '18 at 05:12
  • Request using httpclient and hostname to ip both are not working – Shashi kumar S May 17 '18 at 05:49
  • You said yourself that hostname resolution is working. `https://www.example.com` is not a hostname and so cannot work with `GetHostAddress`. But `www.example.com` is hostname, and it is working. – Evk May 17 '18 at 05:50
  • Ok, I am using `httpClient.postAsync(url,postBodyContent)` It is working if url is `www.example.com` but not working for `https://www.example.com` and `http://www.example.com` – Shashi kumar S May 17 '18 at 06:20

1 Answers1

1

System.Net.Dns.GetHostAddresses does hostname resolution. It is very similar to ping in command line. You can test, that using

ping www.example.com

Will give you response while

ping https://www.example.com

will not.

As for the fact that you cannot issue a HttpClient request to http://www.example.com, it might be because you don't have the appropriate capability set. Go to Package.appxmanifest, click the Capabilities tab and check the Private Networks (Client & Server) capability.

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
  • Client & server capability is enabled, there is a global DNS for `https://www.example.com`. `httpClient` is working for global DNS. For local hosts error is `System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll` – Shashi kumar S May 17 '18 at 16:56
  • I'm thinking - if GetHostAddresses works, why don't you first retrieve the IP and then do http client request to that IP directly? Doesn't that work either? – Martin Zikmund May 17 '18 at 18:27