Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Welcome Guest | Log In | Register | Benefits
Channels ▼
RSS

JVM Languages

Making HTTP Requests From Java



Related Reading


More Insights




eric_j_bruno

Thanks for the feedback and useful information. I apologize if you feel misled. This code was part of a larger project where the Apache classes were used for multiple reasons. Additionally, I wanted to focus on what could be done with the current version of Java, not the next one (JDK 8). However, you've raised some good points and I plan to update this blog with a fresh approach. Thank you.

phansson

Wauw. Somewhat surprised to see you pull in so many external libraries. I must admit that I feel you lead people a bit astray in your posting.

1. As for Base64 encoding.

This can be done using JAXB class which is part of standard JDK since Java 6 so no need to resort to something else.
Just do:

String encodedString = javax.xml.bind.DatatypeConverter.printBase64Binary(myString.getBytes());

In Java8 there will finally be a base64 encoder as part of the Util class. Until then you can use the JAXB class (even if your application doesn't use JAXB at all).

2. Non-disclosure of password

The only way to secure that your password remains secure between you and the endpoint is to use HTTPS rather than HTTP. The fact that the password is Base64 encoded doesn't really help much in terms of security.

3. HTTP client library

It is true that many people resort to Apache Http Client library for making http requests from Java but the alternative to that is not low-level Sockets but to use the regular JDK classes for HTTP client communication, namely HttpURLConnection and HttpsURLConnection from the standard Java lib. These classes have improved over the years and I personally do not have a need to use Apache Http Client library. These standard JDK classes do in fact support proxy.... as you would expect. Just Google it.