5

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?

2 Answers 2

12

Yes, the method you want is setRequestProperty.

Sign up to request clarification or add additional context in comments.

Comments

10

The method you want is setRequestProperty, but 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

4 Comments

Yes, httpclient is de-facto standard for accessig HTTP from Java.
Thanks, thats probably the best way to do it, but I should have mentioned I need to stick to the standard java libraries
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 (hc.apache.org/httpcomponents-core/httpcore/apidocs/org/apache/…).
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.