Set response encoding with HttpClient 3.1

前端 未结 4 714
甜味超标
甜味超标 2021-01-14 14:32

I\'m using org.apache.commons.httpclient.HttpClient and need to setup response encoding (for some reason server returns incorrect encoding in Content-Type). My

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-14 15:07

    Disclaimer: I'm not really knowing HttpClient, only reading the API.

    I would use the execute method returning a HttpResponse, then .getEntity().getContent(). This is a pure byte stream, so if you want to ignore the encoding told by the server, you can simply wrap your own InputStreamReader around it.


    Okay, looks like I had the wrong version (obviously, there are too much HttpClient classes out there).

    But same as before, just located on other classes: the HttpMethod has a getResponseBodyAsStream() method, around which you can now wrap your own InputStreamReader. (Or get the whole array at once, if it is not too big, and convert it to String, as you wrote.)

    I think trying to change the response and letting the HttpClient analyze it is not the right way here.


    I suggest sending a message to the server administrator/webmaster about the wrong charset, though.

提交回复
热议问题