What exactly returns EntityUtils.toString(response)?

前端 未结 2 2019
南旧
南旧 2021-01-06 17:35

I\'m doing my API REST and I saw that in a lot of examples people use EntityUtils.toString(response) to get their response into a String

相关标签:
2条回答
  • 2021-01-06 17:43

    Beware the default charset of EntityUtils.toString:

    Get the entity content as a String, using the provided default character set if none is found in the entity. If defaultCharset is null, the default "ISO-8859-1" is used.

    org.apache.http.util.EntityUtils.toString(response.getEntity, "UTF-8");

    0 讨论(0)
  • 2021-01-06 18:05

    An HttpEntity represents the content of the body of an HTTP response. EntityUtils.toString(HttpEntity) interprets that content as a String and returns it to you.

    If your HTTP response is something like this

    HTTP/1.1 200 OK
    Content-Type: text/xml; charset=utf-8
    Content-Length: 80
    
    <?xml version="1.0" encoding="utf-8"?>
    <root>
        <nested attr="whatever" />
    </root>
    

    Then the String returned by EntityUtils.toString(HttpEntity) will simply contain

    <?xml version="1.0" encoding="utf-8"?>
    <root>
        <nested attr="whatever" />
    </root>
    
    0 讨论(0)
提交回复
热议问题