Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 657
tigger Avatar asked Jan 26 '09 15:01

tigger


People also ask

How do I set headers in HttpURLConnection?

URL url = new URL(urlToConnect); HttpURLConnection httpUrlConnection = (HttpURLConnection) url. openConnection(); Step 2: Add headers to the HttpURLConnection using setRequestProperty method.

How do you add a header to an HTTP request in Java?

Instead of setting the Header on each and every request, we can also configure it as a default header on the Client itself: Header header = new BasicHeader(HttpHeaders. CONTENT_TYPE, "application/json"); List<Header> headers = Lists. newArrayList(header); HttpClient client = HttpClients.

What is the difference between URLConnection and HttpURLConnection?

URLConnection is the base class. HttpURLConnection is a derived class which you can use when you need the extra API and you are dealing with HTTP or HTTPS only. HttpsURLConnection is a 'more derived' class which you can use when you need the 'more extra' API and you are dealing with HTTPS only.


2 Answers

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.

like image 148
Tom Hawtin - tackline Avatar answered Oct 18 '22 03:10

Tom Hawtin - tackline


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.

like image 2
j pimmel Avatar answered Oct 18 '22 04:10

j pimmel