views:

2405

answers:

2

Im trying to improve the Java Html Document a little but i'm running into problems with the HttpUrlConntion. One thing is that some servers block a request if the user agent is a Java VM. Another problem is that the HttpUrlConnection does not set the Referrer or Location header field. Since several sites use these fields to verify that the content was accessed from their own site, I'm blocked here as well. As far as I can see the only resolution is to replace the URL handler of the HTTP protocol. Or is there any way to modify the default HTTP Handler?

+3  A: 

Open the URL with URL.openConnection. Optionally cast to HttpURLConnection. Call URLConnection.setRequestProperty/addRequestProperty.

The default User-Agent header value is set from the "http.agent" system property. The PlugIn and WebStart allow you to set this property.

Tom Hawtin - tackline
Be sure to distinguish between addRequestProperty and setRequestProperty where appropriate. The set variant overwrites any existing header with the same key. The add variant adds and additional instance of the header if one already exists.
laz
+1  A: 

If you use Apache HttpClient to manage your programmatic HTTP connectivity you get an extremely useful API which makes creating connections (and optional automatic re-connecting on fail), setting Headers, posts vs gets, handy methods for retrieving the returned content and much much more.

j pimmel