Вы находитесь на странице: 1из 2

How to call a rest webservice with an

untrusted SSL certicate


by Rodrigo Asensio Nov. 16, 12 IoT Zone

Access the survey results'State of Industrial Internet Application Development'to


learn about latest challenges, trends and opportunities with Industrial IoT, brought to
you in partnership with GE Digital.
In our development environments we have congured self signed SSL certicates for our
applications. Obviously we won't spend money for internal dev servers. So, what we do is self
sign certicates and there is where the problems starts.
I use Jersey rest client to call rest webservices and this is the workaround that I did to get to
actual dev service with the self signed certicate.
I hope this works for you. Follow me on twitter @rasensio or visit my site
www.rodrigoasensio.com
1

TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

public X509Certificate[] getAcceptedIssuers() {

return null;

public void checkClientTrusted(X509Certificate[] certs, String authType) {}

public void checkServerTrusted(X509Certificate[] certs, String authType) {}

} };

8
9

SSLContext context = SSLContext.getInstance("TLS");

10

context.init(null, trustAllCerts, new SecureRandom());

11

HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());

12
13

ClientConfig config = new DefaultClientConfig();

14

config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties

15

@Override

16

public boolean verify(String s, SSLSession sslSession) {

17

return true;

18

19

}, context));

20
21

Client client = Client.create(config);

22

client.setFollowRedirects(true);

22

client.setFollowRedirects(true);

23

WebResource resource = client.resource("https://myserver/myws");

24

resource.accept(MediaType.APPLICATION_JSON_TYPE);

25

String result = resource.post(String.class);

The IoT Zone is brought to you in partnership with GE Digital. Discover how IoT
developers are using Predix to disrupt traditional industrial development models.

Like This Article? Read More From DZone


Using Scalatest for Bi-Directional
Client SSL Tests

Is a MySQL Connection Using SSL or


Not?

Introduction to SSL for Managers

Free Resource

Getting Started With MQTT


Topics: SSL

Opinions expressed by DZone contributors are their own.

Subscribe

Вам также может понравиться