从响应中提取JSON作为ResponseEntityProxy{[Content-Type:application/json; charset=UTF-8,分块:[true]}

3duebb1j  于 2023-08-08  发布在  其他
关注(0)|答案(2)|浏览(313)

我试图上传文件到一个网址,并已收到,而不是常规的JSON字符串响应,只有ResponseEntityProxy{[Content-Type: application/json;charset=UTF-8,Chunked: true]}。正如我所理解的,有一个JSON字符串作为响应,我需要以某种方式提取它。
下面是我的代码,我已经尝试到目前为止做(这是在我上传文件到url的方法中):

public String uploadDocument() {
    
    String responseMsg="empty";
     
    try(CloseableHttpClient client = HttpClients.createDefault()){
    
        HttpPost httpPost = new HttpPost(SupportUtil.WHATSAPP_PUSH_BASEURL+"/media/upload/");
        httpPost.addHeader("Content-type", "application/octet-stream");
        httpPost.addHeader("Authorization", "Bearer " + SupportUtil.WHATSAPP_PUSH_KEY);
        
        File file=null;
        try {
            file = ResourceUtils.getFile("/home/ubuntu/DanFagin.pdf");
        } catch (FileNotFoundException e2) {
            
            e2.printStackTrace();
        }
        
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addBinaryBody("file",file,ContentType.APPLICATION_OCTET_STREAM,"DanFagin.pdf");
        
        org.apache.http.HttpEntity multipart = builder.build();
        httpPost.setEntity(multipart);

        try (CloseableHttpResponse response = client.execute(httpPost)) {
            
            System.out.println(response.getEntity().toString());
            responseMsg= EntityUtils.toString(response.getEntity(),"UTF-8");
            System.out.println(responseMsg);
            
        } catch (ClientProtocolException e) {
            
            e.printStackTrace();
        } catch (IOException e) {
            
            e.printStackTrace();
        }
    
        System.out.println("Response is closed");
        System.out.println(responseMsg);
        
     } catch (IOException e1) {
        
        e1.printStackTrace();
    }
       
    System.out.println("Client is closed");
    return responseMsg;
    
}

字符串
因此,在上面的代码中,我已经构建了HttpResponse的实体,然后尝试使用EntityUtils.toString(entity)方法来获取实体的内容。但是当我使用该方法时,我得到以下错误:

org.apache.http.ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected
    at org.apache.http.impl.io.ChunkedInputStream.getChunkSize(ChunkedInputStream.java:263)
    at org.apache.http.impl.io.ChunkedInputStream.nextChunk(ChunkedInputStream.java:222)
    at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:183)
    at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:135)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
    at java.io.InputStreamReader.read(InputStreamReader.java:184)
    at java.io.Reader.read(Reader.java:140)
    at org.apache.http.util.EntityUtils.toString(EntityUtils.java:227)
    at org.apache.http.util.EntityUtils.toString(EntityUtils.java:270)
    at org.apache.http.util.EntityUtils.toString(EntityUtils.java:290)
    at com.wingsure.WSWhatsapp.repository.RestTemplateImpl.uploadDocument(RestTemplateImpl.java:564)
    at com.wingsure.WSWhatsapp.service.RestTemplateService.uploadDocument(RestTemplateService.java:49)
    at com.wingsure.WSWhatsapp.repository.WebHookRepository.getWebHookDetails(WebHookRepository.java:60)
    at com.wingsure.WSWhatsapp.service.ProductService.getWebHookDetails(ProductService.java:84)
    at com.wingsure.WSWhatsapp.controller.ProductController.getWebHookDetails(ProductController.java:57)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
etc


第564行是EntityUtils.toString()方法所在的位置,如果没有它,我不会收到任何错误。在另一边,没有找到任何其他方式如何可以获得实体的内容,即. JSON字符串,不使用EntityUtils.toString()方法。
有人知道这里有什么问题吗?我将非常感谢任何帮助和建议。谢谢你在每一个案件。

9rbhqvlz

9rbhqvlz1#

尽量不要关闭连接

httpclient.close();
response.close();

字符串
让框架为您关闭它。我不记得以前关过这个。只是我的建议。

j7dteeu8

j7dteeu82#

我的猜测是,你正在从上面的代码中阅读下面3行中的第一行中的服务器响应,第二行失败,因为实体读取器在第一次读取时关闭了连接:

System.out.println(response.getEntity().toString());
        responseMsg= EntityUtils.toString(response.getEntity(),"UTF-8");
        System.out.println(responseMsg);

字符串

相关问题