Click here to Skip to main content
15,891,696 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I am trying to validate url.
I'm using HttpWebRequest and HttpWebResponse for this but in each case i'm getting an exception.

Following is my code for validation

Uri _url = new Uri(TextUrl);
            MessageBox.Show(_url.ToString()); //jus to check url content
            HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(_url);
                try
                {
                 HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
}


catch
                {
                    MessageBox.Show("this url is not valid");
                    TextUrl = "";
                    url.Focus();
}


When I'm entring url in proper syntax like "http://www.gmail.com"
I'm getting customized exception.

But if I'm entering url like "www.gmail.com"
its throwing an error :sigh:
Please reply soon if anyone have any ideas.
Posted
Updated 4-Oct-10 21:38pm
v3
Comments
Dalek Dave 5-Oct-10 3:38am    
Edited for Grammar, Spelling and Readability.

1 solution

Two issues:

1. if you enter www.gmail.com in the text box, you have to check the URL format in programmatic way and add the (http://) portion so that, the input URL actually become http://www.gmail.com before calling WebRequest.Create(url);


2. I have tested the code with setting the url to "http://www.gmail.com" and it works quite fine (Doesn't throw any exception). You please try to debug your code and check whether there is any other code that throws exception. Also, you need to make sure that your machine has Internet connection enable so that your code actually is able to hit a web request.

also, you should modify the try..catch portion so that, you can view the exact exception detail as follows:

try
{
..
}
catch(Exception ex)
{
    MessageBox.Show(ex.ToString());
}
 
Share this answer
 
Comments
Ashutosh kumar Tripathi 5-Oct-10 5:16am    
hi shubho... thanx for ur reply.. but my prob is still same.. actually i m creating a validator for win mobile. is this nything to do with platform?
Al-Farooque Shubho 5-Oct-10 5:20am    
Yes, it may be a connectivity issue from within the platform you are executing the code. Try to see the exception detail message to understand the actual problem.
Ashutosh kumar Tripathi 5-Oct-10 5:27am    
becoz same code i tried to run for simple windows form application.. its working there.... but its not working for windows mobile platform

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900