Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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..

Sun Certified Web component Developer,

Microsoft Certified Solution Developer .NET,

Software Engineer

Posted
try downloading the wsdl file 1st.

How can I try to download?

 

I've used only VS.NET to get WSDL. so I don't know how to download it programmatically!

Sun Certified Web component Developer,

Microsoft Certified Solution Developer .NET,

Software Engineer

Posted

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.

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;
			}
		}
	}
}
}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...