1

I am receiving a two different response for below HTTPPOST request based on the JDK version. On JDK 1.7 it says BAD Request, I am not able to understand the difference in those two responses. Could you please help me to understand the issue?


Reference: Verify the REST Environment (REST-Based APIs)

Request:

HttpClient httpclient = HttpClientBuilder.create().build();

/* Assemble the login request URL */
String loginURL = LOGINURL +
                  GRANTSERVICE +
                  "&client_id=" + CLIENTID +
                  "&client_secret=" + CLIENTSECRET +
                  "&username=" + USERNAME +
                  "&password=" + PASSWORD;
/* POST Login requests */
HttpPost httpPost = new HttpPost(loginURL);

HttpResponse response = null;
/* Execute the login POST request */
response = httpclient.execute(httpPost);
/* verify response is HTTP OK */
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) 
{
    System.out.println("Error authenticating to Force.com: " + statusCode);
    // Error is in EntityUtils.toString(response.getEntity())
    return;
}

Response (JDK 1.8):

HttpResponseProxy {
    HTTP / 1.1 200 OK
    [
        Date : Fri, 21 Oct 2016 10 : 12 : 38 GMT,
        Strict - Transport - Security : max - age = 31536000;
        includeSubDomains,
        Set - Cookie : BrowserId = 3A1ryE8eQa6O - AqbTHyJqQ;
        Path = /;
        Domain=.salesforce.com;Expires=Tue, 20-Dec-2016 10:12:38 GMT,
        Expires: Thu, 01 Jan 1970 00:00:00 GMT,
        Pragma: no-cache,
        Cache-Control: no-cache,
        no-store, 
        X-ReadOnlyMode: false,
        Content-Type: application/json;
        charset = UTF - 8,
        Transfer - Encoding : chunked
    ]
    org.apache.http.client.entity.DecompressingEntity @ 892d68
}

Response (JDK 1.7):

HttpResponseProxy {
    HTTP / 1.1 400 Bad Request
    [
        Date : Fri, 21 Oct 2016 10 : 22 : 05 GMT,
        Strict - Transport - Security : max - age = 31536000;
        includeSubDomains,
        Set - Cookie : BrowserId = tbJli6t8SyCbA6I8eA0vow;
        Path = /;
        Domain=.salesforce.com;Expires=Tue, 20-Dec-2016 10:22:05 GMT,
        Expires: Thu, 01 Jan 1970 00:00:00 GMT,
        Pragma: no-cache,
        Cache-Control: no-cache,
        no-store,
        X-ReadOnlyMode: false,
        Content-Type: application/json;
        charset = UTF - 8,
        Transfer - Encoding : chunked
    ]
    ResponseEntityProxy {
        [
            Content - Type : application / json;
            charset = UTF - 8, Chunked : true
        ]
    }
}
1

The response body should give you more details, but given the works/fails based on JDK version, I'd say you're hitting a TLS 1.2 required problem. You'll need to either stick to JDK 1.8 [where TLS 1.2 is enabled by default], or if you need to support JDK 1.7 as well, then there are flags to enable TLS 1.2 support there, there are details in the KB Article.

  • can you help me with the code. I tried all the code possible mentioned in the article. Still no success. – Alok Oct 24 '16 at 14:24
  • You'll have to check the docs for the http library you're using. Also you should really consider just not using JDK 1.7, its already 1 year past its end of life. java.com/en/download/faq/java_7.xml – superfell Oct 24 '16 at 14:29
  • Yeah, when support for TLS 1.0 was dropped a lot of integrations broke :) – mkorman Oct 26 '16 at 9:46
1

@superfell I got solution. Following code did the job for me. Thanks to you who pushed me in the right direction.

SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
                    SSLContext.getDefault(),
                    new String[] { "SSLv2Hello","SSLv3","TLSv1","TLSv1.1","TLSv1.2"},
                    null,
                    SSLConnectionSocketFactory.getDefaultHostnameVerifier());
            Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
                    .register("http", PlainConnectionSocketFactory.getSocketFactory())
                    .register("https", socketFactory)
                    .build();

            PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
            CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build();

Reference: Enable TLSv1.2 for Apache HTTPClients

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.