Java Reference
In-Depth Information
import
import
org.apache.http.conn.scheme.*
org.apache.http.conn.scheme.*
;
import
import
org.apache.http.conn.ssl.*
org.apache.http.conn.ssl.*
;
import
import
org.apache.http.impl.client.DefaultHttpClient
org.apache.http.impl.client.DefaultHttpClient
;
public
public class
class
MyClient
MyClient
{
public
public final
final static
static
void
void
main
(
String
[]
args
)
throws
throws
Exception
{
DefaultHttpClient client
=
new
new
DefaultHttpClient
();
KeyStore trustStore
=
KeyStore
.
getInstance
(
KeyStore
.
getDefaultType
());
FileInputStream instream
=
new
new
FileInputStream
(
new
new
File
(
"my.keystore"
));
try
try
{
trustStore
.
load
(
instream
,
"changeit"
.
toCharArray
());
}
finally
finally
{
instream
.
close
();
}
SSLSocketFactory socketFactory
=
new
new
SSLSocketFactory
(
trustStore
);
Scheme scheme
=
new
new
Scheme
(
"https"
,
socketFactory
,
443
);
client
.
getConnectionManager
()
.
getSchemeRegistry
().
register
(
scheme
);
HttpGet httpget
=
new
new
HttpGet
(
"https://localhost/"
);
...
proceed with the invocation
...
}
}
Advantages and Disadvantages
Apache HttpClient is a more complete solution and is better designed than
java.net.HttpURLConnection
. Although you have to download it separately from the
JDK, I highly recommend you take a look at it. It has none of the disadvantages of
Ht-
tpURLConnection
, except that it is not JAX-RS-aware. Many JAX-RS implementations, in-
cluding RESTEasy, allow you to use Apache HttpClient as the underlying HTTP client en-
gine, so you can get the best of both worlds.
RESTEasy Client Proxies
The RESTEasy Client Proxy Framework is a different way of writing RESTful Java clients.
The idea of the framework is to reuse the JAX-RS annotations on the client side. When you