Introduction
This blog gives a simple method to check if a web service is up and connections can be made to it.
Overview
The below method will check if we are able to connect to a web service with the URL "url".
You can pass the web service url to the TestConnections method and the method will return success or failure.
The method uses simple HttpWebRequest and HttpWebResponse to check if the url is up.
-
-
-
-
-
- public string TestConnection(string url)
- {
- try
- {
-
- HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
- request.Timeout = 30000;
- using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
- {
- if (response.StatusCode != HttpStatusCode.OK)
- {
- Logger.Log(LogCategory.Business, LogLevel.Warning, Constants.ConnectionFailure);
- return Constants.ConnectionFailure;
- }
- else
- {
- return Constants.ConnectionSuccess;
- }
- }
-
- catch (WebException ex)
- {
- Logger.Log(LogCategory.App, LogLevel.Warning, string.Format(CultureInfo.InvariantCulture, "Error
ing connection to web service at \"{0}\":\r\n{1}", url, ex)); - throw;
- }
- catch (Exception ex)
- {
- Logger.Log(LogCategory.App, LogLevel.Warning, string.Format(CultureInfo.InvariantCulture, "Error test
- ing connection to web service at \"{0}\":\r\n{1}", url, ex));
- throw;
- }
- }