Is there a way to verify Webservice url if it's exists or not?

goodmorningsky

Centurion
Joined
Aug 18, 2003
Messages
172
Hi all
Is there a way to verify Webservice url if it's exists or not?

I have a situation that I have to verify webservice url retrieved from config file before calling Webservice.

Thank you..
 
Here are the two simplest ways to do it using the bcl. The WebClient ,as simple as it is, is unfortunately a GUI component and therefore extends IDisposable. The HttpWebRequest object is more configurable than the WebClient, for example, you can't set the keep-alive property using the WebClient.
Visual Basic:
using System;
using System.Net;

namespace ConsoleApplication2
{
	class Class1
	{
		static void Main(string[] args)
		{
			Uri badUri = new Uri("http://www.webservicex.net/WeatherForecast.asewr");
			Uri goodUri = new Uri("http://www.webservicex.net/WeatherForecast.aspx?WSDL");

			Console.WriteLine("WSDL exists @ {0} = {1}", badUri, WsdlExists1(badUri));
			Console.WriteLine("WSDL exists @ {0} = {1}", badUri, WsdlExists2(badUri));

			Console.WriteLine("WSDL exists @ {0} = {1}", goodUri, WsdlExists1(goodUri));
			Console.WriteLine("WSDL exists @ {0} = {1}", goodUri, WsdlExists2(goodUri));
		}

		static bool WsdlExists1(Uri uri)
		{
			using(WebClient wc = new WebClient())
			{
				try
				{
					wc.DownloadData(uri.AbsoluteUri);
					return true;
				}
				catch(WebException error)
				{
					HttpWebResponse response = (HttpWebResponse) error.Response;
					switch(response.StatusCode)
					{
						case HttpStatusCode.NotFound:
							return false;
						default:
							throw;
					}
				}
			}
		}

		static bool WsdlExists2(Uri uri)
		{
			HttpWebRequest request = (HttpWebRequest) WebRequest.Create(uri);
			try
			{
				using(HttpWebResponse response = (HttpWebResponse) request.GetResponse());
				return true;
			}
			catch(WebException error)
			{
				HttpWebResponse response = (HttpWebResponse) error.Response;
				switch(response.StatusCode)
				{
					case HttpStatusCode.NotFound:
						return false;
					default:
						throw;
				}
			}
		}
	}
}
 
Back
Top