0

I have a WPF app written in c# and it is supposed to work in offline and online both. I have a class that checks for connectivity to web service using following skeleton code :-

        var url = "https://somesite.com/mywebservice.svc";
        var myRequest = (HttpWebRequest)WebRequest.Create(url);
        var response = (HttpWebResponse)myRequest.GetResponse();
        if (response.StatusCode == HttpStatusCode.OK)
            {
             Debug.Write(string.Format("{0} Available", url));
            }
        else
           {
               // error
           }

The issue is the web service requires authentication and hence an "unauthorized" exception is always thrown. Although the exception means the service is available but it is logged at network level and causes security alarm as the check is every 10 seconds. Is there a way to check the authenticated service availability without use of credentials or any other solution to above problem?

Thank you for the help!

AjS
  • 341
  • 2
  • 13
  • can you change the webservice? add a new method? – Mate Jul 02 '18 at 04:10
  • btw, you could send basic auth credentials https://stackoverflow.com/questions/4334521/httpwebrequest-using-basic-authentication – Mate Jul 02 '18 at 04:11
  • How do you define available? That the port is open, or that you get a valid http response? – OneCricketeer Jul 02 '18 at 04:14
  • @Mate i don't want to hard-code credentials as it is some what unsafe; I can add a new method but I believe anonymous access to method will also be not allowed as the service is authenticated. – AjS Jul 02 '18 at 04:14
  • @cricket_007 valid http response – AjS Jul 02 '18 at 04:17
  • Why hardcoded credentials? The common way is adding a health check method. – Mate Jul 02 '18 at 04:18
  • 1
    @Mate is right. Also OP make sure you close the Response object otherwise it works intermittently, Ref https://stackoverflow.com/a/3939689/495455 – Jeremy Thompson Jul 02 '18 at 04:27
  • @Mate can you point me to some resource or examples showing health check? – AjS Jul 02 '18 at 04:36
  • For Wcf service? – Mate Jul 02 '18 at 04:38
  • Hrmm, this is kind of a circular question.. How to check if you get a valid response to a webservice that is secured.. An unauthenticated request is not good enough because it logs an unauthorised exception, but you don't want add authorisation... i think something has to give here potentially – TheGeneral Jul 02 '18 at 04:47
  • instead url "https://somesite.com/mywebservice.svc" you can use "https://somesite.com" to check if it is online – Nitin Sawant Jul 02 '18 at 05:38

0 Answers0