简体   繁体   English

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

[英]Extracting JSON from response as ResponseEntityProxy{[Content-Type: application/json;charset=UTF-8,Chunked: true]}

I am trying to upload file to an url, and have received instead of regular JSON string response, only ResponseEntityProxy{[Content-Type: application/json;charset=UTF-8,Chunked: true]} .我正在尝试将文件上传到 url,并且收到的不是常规的 JSON 字符串响应,只有ResponseEntityProxy{[Content-Type: application/json;charset=UTF-8,Chunked: true]} As I understood, there is JSON string as a response in there and I need to extract it somehow.据我所知,那里有 JSON 字符串作为响应,我需要以某种方式提取它。
Here is my code what I have tried so far to do (this is in the method where I am uploading file to an url):这是我到目前为止尝试做的代码(这是我将文件上传到 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;
    
}

So here above I've built the entity of HttpResponse (earlier in code), and then trying to use EntityUtils.toString(entity) method for getting content of the entity.所以在上面我已经构建了 HttpResponse 的实体(前面的代码),然后尝试使用EntityUtils.toString(entity)方法来获取实体的内容。 But when I use the method, I get the following error:但是当我使用该方法时,出现以下错误:

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

Line 564 is where the EntityUtils.toString() method is, and without it, I don't receive any error.第 564 行是EntityUtils.toString()方法所在的位置,没有它,我不会收到任何错误。 On the other side, didn't find any other way how can I obtain the content of the entity, ie.另一方面,没有找到任何其他方法如何获取实体的内容,即。 the JSON string, without using EntityUtils.toString() method. JSON 字符串,不使用EntityUtils.toString()方法。
Does someone perhaps know what can be issue here?有人可能知道这里有什么问题吗? I would greatly appreciate any help and suggestion.. Thank you in each case.我将不胜感激任何帮助和建议。在每种情况下都谢谢你。

Try not to close the connection尽量不要关闭连接

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

Let the framework close it for you.让框架为您关闭它。 I never remember closing this before.我从来不记得以前关闭过这个。 Just my suggestion.只是我的建议。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Express:从 Content-Type "application/json; charset=utf-8" 中删除 charset=utf-8 - Express: Remove charset=utf-8 from Content-Type "application/json; charset=utf-8" 如何将请求和响应“Content-Type”设置为“application/json;charset=UTF-8”? - How to set Request and response “Content-Type” to “application/json;charset=UTF-8”? 内容类型为application / json; 响应消息的charset = utf-8与绑定的内容类型不匹配(text / xml; charset = utf-8) - The content type application/json; charset=utf-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8) 内容类型'application / json; charset = utf-8'不是预期的类型'text / xml; 字符集= UTF-8' - the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8' 如何将mappingJacksonHttpMessageConverter的内容类型从application / json; charset = UTF-8更改为application / json - How to change content type of MappingJacksonHttpMessageConverter from application/json;charset=UTF-8 to application/json Spring REST:HttpMediaTypeNotSupportedException:内容类型'application / json; charset = UTF-8' - Spring REST: HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' 不支持内容类型 'application/json;charset=UTF-8' - Content type 'application/json;charset=UTF-8' not supported 无法在Node js中使用“ content-type”:“ text / plain; charset = UTF-8” - Unable to Print a JSON Object in Node js with “content-type”: “text/plain; charset=UTF-8” 为什么内容类型标头用于json? “应用程序/ JSON; charset = utf-8“或”application / json“? - Why content type header to use for json? “application/json; charset=utf-8 ” or “application/json”? Rest API 以纯字符串形式发送响应,但内容类型为“application/json;charset=UTF-8”。 我如何阅读回复 - Rest API sends response as plain string however Content Type is "application/json;charset=UTF-8". How can I readthe response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM