Java URLConnection header

Schadenfroh

Elite Member
Mar 8, 2003
38,416
4
0
Greetings,

I am working on a Java client that can interact with an HTTP server and I want to be able to send headers to the server upon connection. The way I know to do it is now deprecated and I do not know the proper method of accomplishing it now. So, I need to figure out how to do something like the following:

Code:
URL url = new URL("www.anandtech.com");
URLConnection connection = url.openConnection();
connection.addRequestProperty("property name", "value");

The problem is that I need to set that header BEFORE I establish a connection. I do not know how to initialize URLConnection without establishing a connection and I need to initialize it in order to set it. Any ideas on how I can go about doing this?

Thanks
 

Schadenfroh

Elite Member
Mar 8, 2003
38,416
4
0
I found a way to do it with Jakarta. Would be nice to find a way to do it with just the standard java libraries.

http://hc.apache.org/httpclient-3.x/tutorial.html
Code:
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
    		new DefaultHttpMethodRetryHandler(3, false));
Change that to whatever you need stated in the header.