0

I'm trying to test if a intranet web application is up using C#. And i tried it using the below code, but it gave me a 401 error though i have access to the app. And in the Angelfish logs i do not see user name for the get request. How can i improve the below code to create the get request as myself.

HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
// Sends the HttpWebRequest and waits for a response.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)
        Console.WriteLine("\r\nResponse Status Code is OK and StatusDescription is: {0}",
                          myHttpWebResponse.StatusDescription);
// Releases the resources of the response.
myHttpWebResponse.Close();
dcg
  • 4,187
  • 1
  • 18
  • 32
Py_learner
  • 53
  • 8
  • why do you think that you have access? are you sending proper headers (auth/user-agent/etc)? – Iłya Bursov Jun 14 '19 at 20:20
  • 1
    Try this answer: https://stackoverflow.com/a/1520722/9365244 – JayV Jun 14 '19 at 20:21
  • when i hit the same url using browser i'm able to access the app, in the request i'm just sending the url and nothing else – Py_learner Jun 14 '19 at 20:23
  • @JayV, thanks for the link - is there a way to fetch just the status code – Py_learner Jun 14 '19 at 20:41
  • @Py_learner The `HttoResponse` object has a property call `StatusCode`. This is the documentation for it: [HttpWebResponse.StatusCode Property](https://learn.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.statuscode?view=netframework-4.8) – JayV Jun 14 '19 at 20:45
  • Thank you @JayV it worked perfectly fine – Py_learner Jun 14 '19 at 21:00

1 Answers1

1

working code -

WebRequest req = WebRequest.Create(url);
req.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
req.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
HttpWebResponse myHttpWebResponse = (HttpWebResponse)req.GetResponse();
if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)
{
Console.WriteLine("\r\nResponse Status Code is OK and StatusDescription is: {0}", myHttpWebResponse.StatusDescription);
}
// Releases the resources of the response.
myHttpWebResponse.Close();
Py_learner
  • 53
  • 8