tigger
tigger

Reputation: 1906

How to modify the header of a HttpUrlConnection

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?

Upvotes: 26

Views: 58850

Answers (3)

Nayanava
Nayanava

Reputation: 129

I solved my problem. We can just send the header to application/json and pass the body as a json object. That simply solves the issue.

Upvotes: 0

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147124

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.

Upvotes: 42

j pimmel
j pimmel

Reputation: 11637

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.

Upvotes: 2

Related Questions