MultipartEntityBuilder and Charset

后端 未结 5 1200
深忆病人
深忆病人 2020-12-09 09:21

I upgraded my httpmime package, and now my strings are not sent or received as UTF-8

MultipartEntityBuilder entity = MultipartEntityBuilder.create();
Charset         


        
相关标签:
5条回答
  • 2020-12-09 09:45

    Solved it :) it turns out that ContentType is now important, and I was sending text that was plain, and also some text that was JSON,

    for the plain text, you can use:

    entity.addTextBody("plain_text",plain_text,ContentType.TEXT_PLAIN);
    

    and for JSON:

    entity.addTextBody("json_text",json_text,ContentType.APPLICATION_JSON);
    

    that way the charset also works on JSON strings (weird, but now OK)

    0 讨论(0)
  • 2020-12-09 09:48

    In my case, I setup the contentType first like this

    ContentType contentType = ContentType.create(
                            HTTP.PLAIN_TEXT_TYPE, HTTP.UTF_8);
    

    and when adding pairs , specify the content type like this

    entityBuilder.addTextBody("title",pic.getTitle(),contentType);
    

    as answered here MultipartEntityBuilder and setCharset for UTF-8 sends empty content

    0 讨论(0)
  • 2020-12-09 09:50

    Please try this for

    Utf-8:

    entity.addTextBody("your text", stringBuffer.toString(), ContentType.create("text/plain", Charset.forName("UTF-8")));
    
    0 讨论(0)
  • 2020-12-09 09:58
    entity.addTextBody("plain_text", plain_text);
    

    will use the default ContentType.TEXT_PLAIN which look like this...

    public static final ContentType TEXT_PLAIN = ContentType.create("text/plain", Consts.ISO_8859_1);
    

    while ContentType.APPLICATION_JSON is using UTF-8 as below

    public static final ContentType APPLICATION_JSON = ContentType.create("application/json", Consts.UTF_8);
    

    So what i did was create our own ContentType like this..

    entity.addTextBody("plain_text", plain_text, ContentType.create("text/plain", MIME.UTF8_CHARSET));
    
    0 讨论(0)
  • 2020-12-09 10:03

    For those who say the accepted solution did not work for them (it didn't for me either), I solved it a different way, using:

    builder.addTextBody(key, שלום, ContentType.TEXT_PLAIN.withCharset("UTF-8"));
    
    0 讨论(0)
提交回复
热议问题