views:

1735

answers:

2

Hi, Is it possible to send a Http header via a URL connection in java? I had this working using sockets, but ran into issues with a firewall which don't seem to be a problem with URLConnection. From looking at the API I get the impression that the output methods in URLConnection are just for filling in forms etc, or can they be used to send my own HTTP headers?

+4  A: 

Yes, the method you want is setRequestProperty.

Matthew Flaschen
+7  A: 

I would recommend to use Apaches HttpClient. With this library you have total control over the request you want to send.

http://hc.apache.org/httpclient-3.x/tutorial.html

HaBaLeS
Yes, httpclient is de-facto standard for accessig HTTP from Java.
stepancheg
Thanks, thats probably the best way to do it, but I should have mentioned I need to stick to the standard java libraries
Simonw
HttpClient is all well in good, but it's not always necessary, and he asked about URLConnection. And you didn't even mention the relevant method, which is HttpMessage.setHeader (http://hc.apache.org/httpcomponents-core/httpcore/apidocs/org/apache/http/HttpMessage.html#setHeader(java.lang.String,%20java.lang.String)).
Matthew Flaschen
Your right I could have mentioned the right Method. But as the Questionnaire was asking playing around with Sockets and UrlConnection its no Problem to point out another alternative which is flexible enough to be prepared for upcoming demands
HaBaLeS