Ioa
Ioa

Reputation: 29

Softlayer API error "java.net.SocketException: Connection reset"

I'm using the java.net API to request the softlayer API.

String url = "https://api.softlayer.com/rest/v3.1/SoftLayer_Account/getInvoices.json";
String query = "objectFilter={\"invoices\":{\"createDate\":{\"operation\":\"betweenDate\",\"options\":[{\"name\":\"startDate\",\"value\":[\"04/01/2017 17:50:40\"]},{\"name\":\"endDate\",\"value\":[\"06/01/2017 17:50:40\"]}]}}}";
HttpsURLConnection conn = (HttpsURLConnection) new URL(url + "?" + query).openConnection();

I get this error

java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:196)
    at java.net.SocketInputStream.read(SocketInputStream.java:122)
    at sun.security.ssl.InputRecord.readFully(InputRecord.java:442)
    at sun.security.ssl.InputRecord.read(InputRecord.java:480)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:934)
    at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:891)
    at sun.security.ssl.AppInputStream.read(AppInputStream.java:102)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:275)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:690)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:633)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:661)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1324)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)

How can I resolve this issue?

Upvotes: 0

Views: 1002

Answers (2)

Albert Camacho
Albert Camacho

Reputation: 1104

Problem is due to characters sent in the request, characters like [] {} : "" , all of them must be encoded, to do that you can use method URLEncoder.encode.

URLEncoder.encode(filter,"UTF-8");

Below is the java code I used to solve the issue. This example is based on https://www.mkyong.com/webservices/jax-rs/restfull-java-client-with-java-net-url/

public static void main(String[] args) {

        // Define user credentials
        String username = "set me";
        String apikey = "set me";

        // Define Service url, method and header params
        String url = "https://api.softlayer.com/rest/v3.1/SoftLayer_Account/getInvoices.json";
        String filter = "{\"invoices\":{\"createDate\":{\"operation\":\"betweenDate\",\"options\":[{\"name\":\"startDate\",\"value\":[\"04/01/2017 17:50:40\"]},{\"name\":\"endDate\",\"value\":[\"06/01/2017 17:50:40\"]}]}}}";

        try {
           String encodedFilter = URLEncoder.encode(filter,"UTF-8");

           // Build the URL request
           URL urlRequest = new URL(url + "?objectFilter=" + encodedFilter);

            HttpsURLConnection conn = (HttpsURLConnection) urlRequest.openConnection();

            // Encode user credentials to Base64 form
            String authString = username + ":" + apikey;
            String authStringEnc = new String(Base64.getEncoder().encode(authString.getBytes()));

            // Add authentication, request method and property
            conn.setRequestProperty("Authorization", "Basic " + authStringEnc);
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "application/json");


            if (conn.getResponseCode() != 200) {
                System.out.println("Failed : HTTP error code : " + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

            String output;
            System.out.println("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                System.out.println(output);
            }

            conn.disconnect();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Upvotes: 1

Fernando Iquiza
Fernando Iquiza

Reputation: 531

Unfortunetaly Object Filters are not yet implemented in the SoftLayer Java Api client, currently there's an open Issue regarding this, see:

https://github.com/softlayer/softlayer-java/issues/30

Meanwhile you may:

Use a Rest request as follows, to retrieve required information:

https://[username]:[apiKey]@api.softlayer.com/rest/v3.1/SoftLayer_Account/getInvoices?objectFilter={"invoices":{"createDate":{"operation":"betweenDate","options":[{"name":"startDate","value":["04/01/2017 17:50:40"]},{"name":"endDate","value":["06/01/2017 17:50:40"]}]}}}

Retrieve getInvoices result and then filter it by using your own code; Or use any other SoftLayer API clients for supported programming languages, please see the following:

http://sldn.softlayer.com/

Upvotes: 0

Related Questions