Thursday, May 5, 2011

How to send an HTTP header in java

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?

From stackoverflow
  • Yes, the method you want is setRequestProperty.

  • 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

    stepancheg : Yes, httpclient is de-facto standard for accessig HTTP from Java.
    Simonw : Thanks, thats probably the best way to do it, but I should have mentioned I need to stick to the standard java libraries
    Matthew Flaschen : 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)).
    HaBaLeS : 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

0 comments:

Post a Comment