Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Testing validity of a URL 1

Status
Not open for further replies.

GMX

Programmer
Mar 21, 2003
33
US
I'm using webservices and in my remote application I access them (duh)
The webservice and the remote app are going to be in a VPN so connection losses are quite possible...
Now my code is like such:

try{
//instantiate webservice, set it's url (ex //call a service's function
}
catch(System.Net.WebException ex)
{//do stuff here}

This function runs on it's own thread but even so the GUI hangs for about 3 seconds if the network is down.

So the question is... is there a more effecient way than exceptions to check if a URL is real/accessible?
 
Open a socket against port 80 and send a CRLF or two to see if you get the HTTP/1.1 400 response back. Or maybe just being able to successfully open the socket is good enough.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Ya but isn't that where the delay comes from in the 1st palce?
 
I'm not sure -- since you were using a relatively high-level component (web service), there could be a lot going on in there that we don't know about. By using a low-level component (sockets), you eliminate a lot of that.

You could also open the socket in asynchronous mode and watch for the connect event.
Code:
// From MSDN:
//
public static void Connect(EndPoint remoteEP, Socket client) {
    client.BeginConnect(remoteEP, 
        new AsyncCallback(ConnectCallback), client );
   connectDone.WaitOne();
}

private static void ConnectCallback(IAsyncResult ar) {
    try {
        // Retrieve the socket from the state object.
        Socket client = (Socket) ar.AsyncState;

        // Complete the connection.
        client.EndConnect(ar);

        Console.WriteLine("Socket connected to {0}",
            client.RemoteEndPoint.ToString());

        // Signal that the connection has been made.
        connectDone.Set();
    } catch (Exception e) {
        Console.WriteLine(e.ToString());
    }
}
If you don't get a connect event in a decent time, or if an exception is thrown, then you know that there's a network problem.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top