2017-05-03 4 views
0

WebサービスへのPOSTリクエストにApache HttpClientを使用します。 私はどんなに体が存在しないHttpClient POST要求は、本体がなくてもステータス200を返します(ただし、これは必須です)。コンテンツの長さは-1

httpResult = 200

を取得しています。私はのようにいくつかのボディがあるはずだと知っています。 別のPOST呼び出しメソッドを使用すると、本体がJSON形式になります。

このメソッドでは、応答本体の長さ= -1です。

response.getEntity()。getContentLength() = -1;

結果EntityUtils.toString(response.getEntity())は空の文字列です。

コードは次のとおりです。

CloseableHttpClient client = HttpClients.createDefault(); 
    HttpPost httpPost = new HttpPost(url); 

    JSONObject attributes = new JSONObject(); 
    JSONObject main = new JSONObject(); 

    attributes.put("201", "Frank"); 
    main.put("attributes", attributes); 
    main.put("primary", "2"); 

    String json = main.toString(); 

    StringEntity entity = new StringEntity(json); 
    httpPost.setEntity(entity); 
    httpPost.setHeader("Accept", "application/json"); 
    httpPost.setHeader("Content-type", "application/json"); 

    CloseableHttpResponse response = client.execute(httpPost); 
    httpResult = response.getStatusLine().getStatusCode(); 

    client.close(); 

    if (httpResult == HttpURLConnection.HTTP_OK) { 

     HttpEntity ent = response.getEntity(); 

     Long length = ent.getContentLength(); 

     System.out.println("Length: " + length);// length = -1 

    } 

誰もが問題を解決するためにどのように私のいくつかのヒントを与えるだろうか?

さらに私は正しい応答体を与えるコードを追加したいと思います。この場合、私はHttpURLConnectionを使用します。

 HttpURLConnection urlConnect = (HttpURLConnection) url.openConnection(); 
     urlConnect.setConnectTimeout(10000); 

     urlConnect.setRequestProperty("Accept", "application/json"); 
     urlConnect.setRequestProperty("Content-Type", "application/json"); 
     urlConnect.setRequestMethod("POST"); 

     JSONObject attributes = new JSONObject(); 
     JSONObject main = new JSONObject(); 

     attributes.put("201", "Frank"); 
     main.put("primary", "2"); 
     main.put("attributes", attributes); 

     urlConnect.setDoOutput(true); 

     OutputStreamWriter wr = new OutputStreamWriter(urlConnect.getOutputStream()); 
     wr.write(main.toString()); 
     wr.flush(); 

     httpResult = urlConnect.getResponseCode(); 

     System.out.println("Http Result: " + httpResult); 

     if (httpResult == HttpURLConnection.HTTP_OK) { 

      InputStream response = urlConnect.getInputStream(); // correct not empty response body 

      ... 
     } 
+0

いただきまし 'EntityUtils.toString(response.getEntity())の結果を'でしょうか? –

+0

** EntityUtils.toString(response.getEntity())**の結果は空の文字列です。 –

+0

あなたは行き​​ます。応答で返されるエンティティはないため、長さは-1です。他のHTTPクライアントを使用しているときに応答を得ることができますか? –

答えて

1

client.close();を最後に移動してください(応答の作業後)。 HttpUrlConnectionから応答を抽出する

し、次

InputStream response = urlConnect.getInputStream(); 
BufferedReader br = new BufferedReader(new InputStreamReader(response)); 
StringBuilder sb = new StringBuilder(); 
String line; 
while ((line = br.readLine()) != null) { 
    sb.append(line+"\n"); 
} 
br.close(); 

JSONObject object = new JSONObject(sb.toString()); //Converted to JSON Object from JSON string - Assuming response is a valid JSON object. 
関連する問題