首页 > 解决方案 > REST webservice call for HTTPS link - 400 Bad Request error

问题描述

Using SOAP UI, I am able to successfully invoke the REST GET call with basic authentication

GET https://app.test.com/testing/rest/authentication-point/authentication HTTP/1.1
Accept-Encoding: gzip,deflate
Authorization: Basic aPOjYVclOmIzABFhZjVpJES=
Host: app.test.com
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

I received response code as 200.

Similar request, When I tried to invoke via java client, it is giving 400 status code.

CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("User", "Password"));
final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);

HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = null;

response = client.execute(
                new HttpGet("https://app.test.com/testing/rest/authentication-point/authentication"),
                context);

int statusCode = response.getStatusLine().getStatusCode();

This code worked properly when the host was HTTP. Recently a VIP is added and made as HTTPS, after which it is not working. Please suggest a fix for this.

标签: javarestbasic-authentication

解决方案


You need to use ssl with http client:

    TrustStrategy acceptingTrustStrategy = (cert, authType) -> true;
SSLSocketFactory sf = new SSLSocketFactory(
  acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("https", 8443, sf));
ClientConnectionManager ccm = new PoolingClientConnectionManager(registry);

CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("User", "Password"));
final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);

DefaultHttpClient httpClient = new DefaultHttpClient(ccm);

HttpGet getMethod = new HttpGet(new HttpGet("https://app.test.com/testing/rest/authentication-point/authentication"),context);

HttpResponse response = httpClient.execute(getMethod);

int statusCode = response.getStatusLine().getStatusCode();

推荐阅读