2016-10-07 13 views
0

我有一個Java應用程序正在對外部REST服務進行HTTP DELETE。此錯誤從服務器回來給我(運行C#):從定製Java客戶端刪除請求的行爲與例如郵差

"Value cannot be null.\r\nParameter name: source\n at System.Linq.Enumerable.Count[TSource](IEnumerable`1 source)\r\n at AppCloud_Framework.Controllers.NotificationItemsController.DeleteNotificationItem(NotificationItem[] notificationItems) in C:\\Users\\jonas\\OneDrive\\VS Projects\\AppCloud Framework\\AppCloud Framework\\Controllers\\NotificationItemsController.cs:line 101\nValue:null" 

的事情是,當我設置郵差使HTTP請求到相同的URL,以相同的有效載荷和相同的HTTP方法,行動是成功的。

我沒有訪問服務器進一步調查,所以我需要從客戶端找到解決方案。無論如何,這似乎是一個客戶端問題。

我一直在嘗試自己找到問題,但沒有成功。我所能想到的是將「application/json」添加到Accept和Content-Type標題屬性。

我的HTTP客戶端:

public static Response execute(String url, Method method, String body) { 
    Response response = new Response(); 

    try { 
     //////////////////////////////////////// 
     // Create a trust manager that does not validate certificate chains 
     TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { 
      public java.security.cert.X509Certificate[] getAcceptedIssuers() { 
       return null; 
      } 
      public void checkClientTrusted(X509Certificate[] certs, String authType) { 
      } 
      public void checkServerTrusted(X509Certificate[] certs, String authType) { 
      } 
     } }; 
     // Install the all-trusting trust manager 
     final SSLContext sc = SSLContext.getInstance("SSL"); 
     sc.init(null, trustAllCerts, new java.security.SecureRandom()); 
     HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); 
     // Create all-trusting host name verifier 
     HostnameVerifier allHostsValid = new HostnameVerifier() { 
      public boolean verify(String hostname, SSLSession session) { 
       return true; 
      } 
     }; 

     // Install the all-trusting host verifier 
     HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); 

     //////////////////////////////////////// 


     URL urlObj = new URL(url); 
     HttpsURLConnection conn = (HttpsURLConnection) urlObj.openConnection(); 

     conn.setInstanceFollowRedirects(false); 
     conn.setRequestMethod(method.toString()); 
     conn.setRequestProperty("Content-Type", "application/json"); 
     conn.setRequestProperty("Accept", "application/json"); 
     //conn.setRequestProperty("Authorization", _authToken); 

     if (method == Method.POST || method == Method.PUT || method == Method.DELETE) { 
      conn.setDoOutput(true); 
      final OutputStream os = conn.getOutputStream(); 
      os.write(body.getBytes()); 
      os.flush(); 
      os.close(); 
     } 

     int status = conn.getResponseCode(); 
     //log.info("HTTP request status code: "+status); 
     InputStream is; 
     if (status>399){ 
      is = conn.getErrorStream(); 
     }else{ 
      is = conn.getInputStream(); 
     } 
     if (is==null) return null; 
     BufferedReader rd = new BufferedReader(new InputStreamReader(is, 
       "UTF-8")); 

     String line; 
     while ((line = rd.readLine()) != null) { 
      response.body += line; 
     } 
     rd.close(); 

     response.statusCode = conn.getResponseCode(); 
     conn.disconnect(); 
    } catch (Exception e) { 
     //log.error(e.getMessage()); 
     e.printStackTrace(); 
     System.out.println(""); 
     response.exception = e.getMessage(); 
    } 
    return response; 
} 

我提出這身搭配的請求(忽略編碼問題,那源是別的地方):

[{"hash":"150a17e99f67ce29fcc600c92eee831d","instanceid":"cb440a6f-44ef-4f05-ab41-143153655b6e","text":"{\"C_FirstAndLastName\":\"und\",\"ContactID\":\"1374231\",\"C_Fax\":\"\"}","queueDate":"2016-10-04T03:18:37"},{"hash":"1a94d9b5acff1a27dfe45be4ca5d9138","instanceid":"fdsfdsf-44ef-4f05-ab41-143153655b6e","text":"{\"C_FirstAndLastName\":\"J?â??rgen\",\"ContactID\":\"323093\",\"C_Fax\":\"fsdfsd-B401-4AD3-AEA1-fdsfsdfsd\"}","queueDate":"2016-10-04T03:18:37"},{"hash":"8e592fb16d464bfd0f90f69818944198","instanceid":"fdsfsdf-44ef-4f05-ab41-143153655b6e","text":"{\"C_FirstAndLastName\":\"Claus\",\"ContactID\":\"2495844\",\"C_Fax\":\"fdsfsdgsd-304D-4E91-8586-fsdfsdfsd\"}","queueDate":"2016-10-04T03:18:37"},{"hash":"d6d226255e62690e50abbfa15c4b5462","instanceid":"cb440a6f-44ef-4f05-ab41-143153655b6e","text":"{\"C_FirstAndLastName\":\"Test J??rgen\",\"ContactID\":\"323093\",\"C_Fax\":\"fdsfsdfsd-B401-4AD3-AEA1-fdsfsdfsdf\"}","queueDate":"2016-10-04T03:18:49"}] 

回答

0

我所要做的就是在輸出流中定義編碼。我不確定是否有人能夠幫助我,因爲我剛剛嘗試了很多東西,其中一些工作,但不幸的是,沒有任何東西指向我這個方向。

os.write(body.getBytes("UTF-8"));