aspose file tools*
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 Java 8 in Action this week in the Java 8 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.
I agree. Here's the link:
subject: URL validation
Similar Threads
How to start a servlet from a Java Class
downloadWebpage() problem
URL connection problem
cannot be resolved error - returning URL
Could not able to open the file in the local system using http protocol