This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes URL validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "URL validation" Watch "URL validation" New topic

URL validation

Ming Chen

Joined: Mar 14, 2002
Posts: 13
I need to check the url user enters in my servlet.
The code on the following works fine for simple url such as "" but if the urlString has query string such as "", Exception throws on url.openStream(), I don't know why, because the url with query string is valid, but it just won't work. Could anyone help. Is there any alternative for this?
// open a connection to the url to get the size of the page
URL url = null;
DataInputStream input = null;
url = new URL(urlString);
InputStream in = url.openStream();
input = new DataInputStream(in);
} catch(Exception e) {
out.println("The url you provided can not be accessed, please try again");
Michael Target

Joined: Jun 25, 2002
Posts: 12
Ming Chen, on query "" your program received HTTP response code 403 (Forbidden) from Google because of Google Terms of Service for Your Personal Use
"No Automated Querying
You may not send automated queries of any sort to Google's system without express permission in advance from Google. Note that "sending automated queries" includes, among other things:
using any software which sends queries to Google to determine how a website or webpage "ranks" on Google for various queries;
"meta-searching" Google; and
performing "offline" searches on Google.
Please do not write to Google to request permission to "meta-search" Google for a research project, as such requests will not be granted"
Ming Chen

Joined: Mar 14, 2002
Posts: 13
Thanks, Michael Target.
You are very helpful. I tried to use query string in url for other webpage, it works fine. But how google knows the query is not sent from web browser.
Michael Target

Joined: Jun 25, 2002
Posts: 12
Google may determine web browsers or something else from an HTTP user agent string.
Don't get me started about those stupid light bulbs.
subject: URL validation
Similar Threads
URL connection problem
How to start a servlet from a Java Class
Could not able to open the file in the local system using http protocol
cannot be resolved error - returning URL
downloadWebpage() problem