Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
132 views
in Technique[技术] by (71.8m points)

java - MultipartEntityBuilder and Charset

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

MultipartEntityBuilder entity = MultipartEntityBuilder.create();
Charset chars = Charset.forName("UTF-8");
entity.setCharset(chars);
entity.addTextBody("some_text", some_text);

HttpPost httppost = new HttpPost(url); 
httppost.setEntity(entity.build());
...and so on..

what am I missing? I used to build a StringBody and set the charset in the stringbody, but that is deprecated now, and it just doesn't seem to work

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...