• Post Reply Bookmark Topic Watch Topic
  • New Topic

How does URL work differently than the browser?  RSS feed

 
Galen Palmer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to verify that a user entered URL is correct so I open a stream and read a byte or two. If there is no IOException I assume that the user's URL is correct and go on my way...

This works 99.9% of the time. When it doesn't, however, I've tried to go back and manually verify the URL. I type it into FireFox and it works through the browser even through the request from the Servlet it throws a 403 error.

Sample code:



Now... I've just run into the same problem using Google Map's Geolocation resource. The basic URL is:

http://maps.google.com/maps/geo?q=123%20Main,%20Anytown,%20MD&output=csv&key=UNIQUE_KEY

Entered into a browser it comes back with a CSV text with the response code, accuracy flag, lat, and lon. From my servlet, however, I'm getting a 400 error.

Anyway, here's the question:

How does Google's web server know that the first request was from a browser and the second request was from within a Tomcat instance? In other words, what's the difference between java.net.URL's request versus the browser's request?
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The user agent header is used to identify who the client is. The geocoder result is strange. I've written many apps using the Google geocoder and not had any problems. The key is unique to a particular URL so that may be the problem.
 
Galen Palmer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the answer. You were right. I was so caught up with the previous problem validating URLs that missed a bug in my maps.google.com URL (kids, always remember to URLEncode your parameters :-)

In response to the request headers, an I right in understanding that you can not set custom request headers using the URL class? Is there a better (more comprehensive) class or library for doing HTTP requests from within Java code?
 
Galen Palmer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A little research and I'm answering my own question. From the javadocs for URL:


If for the URL's protocol (such as HTTP or JAR), there exists a public, specialized URLConnection subclass belonging to one of the following packages or one of their subpackages: java.lang, java.io, java.util, java.net, the connection returned will be of that subclass. For example, for HTTP an HttpURLConnection will be returned...


And, of course, HttpURLConnection seems to have all of the necessary setters.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!