Connection Timed Out: Connect + TRTH Rest API Call

san tang
san tang Explorer

I am getting connection timeout when I try to run the DSS2SearchByRIC.java.
Is there a proxy setting that I need to change in my eclipse?

The postman GET request works just fine.

Best Answer

  • Jirapongse
    Jirapongse ✭✭✭✭✭
    Answer ✓

    I got the same exception after adding a "https" parameter while creating HttpHost.

    HttpHost proxy = new HttpHost(proxyHostName, proxyPortNumber, "https");
    javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?

    However, if I remove the "https" parameter, it works fine.

    HttpHost proxy = new HttpHost(proxyHostName, proxyPortNumber);

Answers

  • Jirapongse
    Jirapongse ✭✭✭✭✭

    Please try to set connection timeout in the code.

        private int timeout=30;
    private RequestConfig config = RequestConfig.custom()
    .setConnectTimeout( timeout * 1000)
    .setConnectionRequestTimeout(timeout * 1000)
    .setSocketTimeout(timeout * 1000).build();
    private CloseableHttpClient httpclient = HttpClientBuilder.create().setDefaultRequestConfig(config).disableContentCompression().build();

    I found this code at http://www.baeldung.com/httpclient-timeout.

  • Thank you for the reply! I still get the same timeout error.

  • @Jirapongsei


    I get the following error:

    javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?

    Any idea why I get that?

  • Yes. We have proxy and I have programmatically setup the system proxy.

  • Jirapongse
    Jirapongse ✭✭✭✭✭

    It may relate to the Proxy. Can you share the code? I will try it in my environment.

  • @Jirapongsei

    Please find below the sample code:

    It's one of the example files in Java tutorials.

    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.util.Properties;


    import org.apache.http.HttpHost;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.impl.client.BasicResponseHandler;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.ResponseHandler;
    import org.apache.http.conn.params.ConnRoutePNames;
    import org.json.JSONObject;
    /**
    * Hello world!
    *
    */
    public class App
    {
    private String urlHost = "https://hosted.datascopeapi.reuters.com/RestApi/v1";
    private String sessionToken = "";
    private CloseableHttpClient httpclient = HttpClientBuilder.create().disableContentCompression().build();


    public static void main(String[] args) throws Exception {
    Properties props = System.getProperties();
    props.setProperty("https.proxyHost", "*****");
    props.setProperty("https.proxyPort", "****");
    // Port Number and Proxy set above
    if(args.length < 2) {
    System.out.println("Please enter DSS2 Username as 1st command line parameter, Password as 2nd");
    System.exit(-1);
    }
    App dss2 = new App();
    dss2.getSessionToken(args[0], args[1]);
    dss2.httpclient.close();
    }
    /**
    * Request DSS2 for a Session Token (24 hour life)
    *
    * @param username
    * @param password
    */

    public void getSessionToken(String username, String password) {


    try {
    HttpPost httppost = new HttpPost(urlHost + "/Authentication/RequestToken");


    httppost.addHeader("content-type", "application/json; charset=UTF-8");


    JSONObject TokenRequest = new JSONObject()
    .put("Credentials", new JSONObject()
    .put("Username", username)
    .put("Password", password));


    StringEntity requestBody = new StringEntity(TokenRequest.toString());


    httppost.setEntity(requestBody);


    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    httpclient = (CloseableHttpClient) createHttpClientOrProxy();
    String response = httpclient.execute(httppost, responseHandler);
    JSONObject jsonResponse = new JSONObject(response);


    sessionToken = jsonResponse.get("value").toString();
    System.out.println("Session Token (expires in 24 hours):\n" + sessionToken);


    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    } catch (ClientProtocolException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    private HttpClient createHttpClientOrProxy() {


    HttpClientBuilder hcBuilder = HttpClients.custom();


    // Set HTTP proxy, if specified in system properties
    if( System.getProperty("https.proxyHost") !=null ) {
    int port = 8080;
    if( System.getProperty("https.proxyPort") !=null ) {
    port = Integer.parseInt(System.getProperty("https.proxyPort"));
    }
    HttpHost proxy = new HttpHost(System.getProperty("https.proxyHost"), port, "https");
    DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
    hcBuilder.setRoutePlanner(routePlanner);
    }


    CloseableHttpClient httpClient = hcBuilder.build();


    return httpClient;
    }
    }
  • Seriously!
    That worked!!

    Thanks a lot. I owe you a treat for sure.