1

I need to check if url exists and can be reached. In order to do it I send Get request and handle the status:

var httpClient = new HttpClient();
var response = httpClient.GetAsync(new Uri(pageUrl));
isPageAccessible = response.Result.StatusCode == HttpStatusCode.OK;

However, the server uses NTLM for authentication. As I found it here, there are several steps (requests) before I get success OK status. For the first request I get 401 Unauthorized status and can't go to further steps.

All in all, how can I check url on the server with NTML upon completion of all requests?

Community
  • 1
  • 1
Alex
  • 13
  • 2
  • this should work - http://stackoverflow.com/a/36842188/5665484. And for checking whether the url is servicable, I would suggest to do a HEAD request instead of GET – Developer Sep 02 '16 at 06:17

2 Answers2

0

You're setting yourself up for failure since there are dozens of reasons why a request may not return a 200 OK response. One may be that the response has no content 204 No Content. Another may be that the request only accepts POST or PUT requests. Another, as you've discovered, may be that it has an authentication system in front of it 401 Not Authorized. Another may be just that the response is a redirect 301 Moved Permanently or 302 Found. Or it could be behind a proxy 305, 306, etc.

The only way you can determine if a URL really exists is to request that the other end prove it. Google does it, Facebook does it, Pinterest does it, etc. The way they do it is they ask the sender to set an MX record in their DNS or a meta tag on their index.html with a custom token they generate. If the token exists, then they're who they say they are.

Anything else is unreliable.

Soviut
  • 88,194
  • 49
  • 192
  • 260
0

If you are accessing an authenticated server, you should provide credentials. Credentials of running process for NTLM can be provided with HttpClient as below:

var handler = new HttpClientHandler { 
                      Credentials = System.Net.CredentialCache.DefaultCredentials
                  };
var httpClient = new HttpClient(handler);
var response = httpClient.GetAsync(new Uri(pageUrl));
Halis S.
  • 446
  • 2
  • 11