Set response encoding with HttpClient 3.1

前端 未结 4 652
甜味超标
甜味超标 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:16

    A few notes:

    1. Server serves data, so it's up to server to serve it in an appropriate format. So response encoding is set by server not client. However, client could suggest to server what format it would like via Accept and Accept-Charset:

      Accept: text/plain
      Accept-Charset: utf-8
      

      However, http servers usually do not convert between formats.

    2. If option 1. does not work, then you should look at the configuration of the server.

    3. When String is sent as raw bytes (and it always is, because this is what networks transmit), there is always the encoding defined. Since server produces this raw bytes, it defines the encoding. So, you can not take raw bytes and use encoding of your choice to create a String. You must use encoding that was used when converted from String to bytes.

提交回复
热议问题