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?
Asked
Active
Viewed 5.8k times
26

QuantumMechanic
- 13,795
- 4
- 45
- 66

tigger
- 1,856
- 3
- 18
- 23
3 Answers
42
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
- 145,806
- 30
- 211
- 305
-
11Be 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 Jan 26 '09 at 19:53
-
Does the `addRequestProperty` adds a new line (in other words the requests will have 2 lines with **identical** keys) or does it append a comma and the new value to the existing line? – Pacerier Dec 26 '11 at 12:45
-
@leoger Actually addRequestProperty will add the key even if the key already exists, so if you call addRequestProperty("User-Agent", "A") then call addRequestProperty("User-Agent", "B"), the headers will include both "User-Agent: A" and "User-Agent: B". laz 's answer is correct. The best way is use Wireshark to capture the packet yourself and see how the headers are. – Daniel Chow Mar 11 '14 at 06:31
-
Actually using http://httpbin.org is probably a better solution than using wireshark. – Jonathan Dec 16 '14 at 14:45
-
1I tried, but it sometimes does not work.. I used Apache HttpClient. It works like a charm. – Kyung Hwan Min Oct 03 '15 at 01:34
2
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
- 11,617
- 6
- 33
- 43
-
12I know this post is old, but if anyone reads it: HttpURLConnection is now preferred over HttpClient, see [this blog post](http://android-developers.blogspot.com/2011/09/androids-http-clients.html) for info. – kamituel Jun 17 '13 at 06:28
0
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.

Nayanava
- 69
- 1
- 6