3

I am trying to authenticate to an external server from Salesforce. The external server hosts a Rest resource and returns data in JSON format. Once authenticated the external server returns an cookie which needs to be used for subsequent GET requests. Below is the Java code for the same. I am trying to convert it to Apex. Any help would be appreciated. Thanks,

    String username="username";
    String password="password";
    HttpClient c = new HttpClient();
    HttpHost host = new HttpHost("hostabc.xyz.com", 443, "https");
    c.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);

    try {
        // login and get the cookie
        HttpGet getLogin = new HttpGet("/Default.aspx");
        getLogin.addHeader("UserName", username);
        getLogin.addHeader("Password", password);
        getLogin.addHeader("Accept", "application/json");
        HttpResponse r = c.execute(host, getLogin);
        System.out.println(r.getStatusLine());
        System.out.println(EntityUtils.toString(r.getEntity()));

        // should make sure you don't get an error message

        HttpGet getReport = new HttpGet("/api/reporting/abc");
        getReport.addHeader("Accept", "application/json");
        HttpResponse r2 = c.execute(host, getReport);
        System.out.println(r2.getStatusLine());
        System.out.println(EntityUtils.toString(r2.getEntity()));
    } catch (IOException e) {
        e.printStackTrace();
    } finally {

    }

Please see the below Apex version of the code used for authentication and making subsequent request using the cookie.

I am getting an error 'System.CalloutException: Remote host closed connection during handshake'.

global class Webservicecallout {

     static final String EndpointURL1='https://hostabc.xyz.com.com/Default.aspx'; 
     static final String EndpointURL2='https://hostabc.xyz.com/api/reporting/abc'; 
     static final String username='username';
     static final String password='password';
     Static string cookies='';

    global static String getReport(){

      // First Authentication request 
      String reportJSON=''; 
      HttpRequest request1 = new HttpRequest();
      request1.setMethod('GET');
      request1.setEndpoint(EndpointURL1);
      request1.setHeader('UserName', username);
      request1.setHeader('Password', password); 
      request1.setHeader('Accept', 'application/json');
      HttpResponse response1 = new Http().send(request1);
      System.debug('@@@@ response1' +response1);
      cookies = response1.getHeader('Set-Cookie');
      System.debug('@@@@ cookies' +cookies);

     // Second request for JSON data.
      HttpRequest request2 = new HttpRequest();
      request2.setMethod('GET');
      request2.setEndpoint(EndpointURL2);
      request2.setHeader('Accept', 'application/json');
      request2.setHeader('Cookie', cookies);
      HttpResponse response2 = new Http().send(request2);
      System.debug('@@@@ response2' +response2);

      return reportJSON;

    }

}
3

The problem is with SSL; your handshake is being rejected. Apex Code is currently using SSL 3.0, which has been disabled by many hosts due to the POODLE vulnerability. Any host that has done so will have connectivity issues when Apex Code attempts to connect to that service. Salesforce is releasing a fix for this no later than December 10th, 2014 to address this.

  • 1
    ... no later, but no earlier either ! ;( – altius_rup Nov 4 '14 at 21:29

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.