2017-07-31 93 views
0

我正在使用java.net API來請求softlayer API。Softlayer API錯誤「java.net.SocketException:連接重置」

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(); 

我得到這個錯誤

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) 

我怎樣才能解決這個問題?

回答

0

Unfortunetaly 對象過濾尚未在SoftLayer的Java API的客戶端來實現,目前有關於這一個開放的問題,請參見:

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

同時,您可以:

藉機休息一下請求如下,以檢索所需信息:

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"]}]}}} 

檢索getInvoices結果,然後使用自己的代碼對其進行過濾;或使用任何其他的SoftLayer API客戶支持的編程語言,請參閱以下內容:

http://sldn.softlayer.com/

1

問題是由於在請求發送的字符,字符,例如[] {}:「」,所有的他們必須編碼,要做到這一點,你可以使用方法URLEncoder.encode

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

下面是我用來解決問題的java代碼。這個例子是基於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(); 
     } 
    } 
+0

謝謝你的回答有沒有更多的錯誤但是結果是錯誤的ObjectFilter的不負責由SOFTLAYER服務器取,我讓所有的發票! – Ioa

+0

我更新了我的答案和示例,調查後發現問題是網址編碼。 –

相關問題