Java 如何使用 apache httpclient fluent 4.3.2 在请求中设置字符集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21984219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:50:03  来源:igfitidea点击:

How to set the charset in request using apache httpclient fluent 4.3.2

javacharacter-encodingrequestapache-httpclient-4.x

提问by Sandy

Request.config(CoreProtocolPNames.HTTP_CONTENT_CHARSET, Consts.UTF_8)and Request.elementCharset(charset)are Deprecated.

Request.config(CoreProtocolPNames.HTTP_CONTENT_CHARSET, Consts.UTF_8)Request.elementCharset(charset)已弃用。

Now how to set the request charset to utf-8 with fluent?

现在如何使用fluent将请求字符集设置为utf-8?

Request.Post(url)
    .useExpectContinue()
    .version(HttpVersion.HTTP_1_1)
    .bodyString("Important stuff", ContentType.DEFAULT_TEXT)
    .execute().returnContent().asBytes();

采纳答案by Rafa Hernández

Try

尝试

Request.setHeader(CoreProtocolPNames.HTTP_CONTENT_CHARSET, Consts.UTF_8);

回答by ok2c

That would be the right way doing it

那将是正确的做法

Request.Post("someurl")
        .useExpectContinue()
        .version(HttpVersion.HTTP_1_1)
        .bodyString("Important stuff", ContentType.create("text/plain", Consts.UTF_8))
        .execute()
        .returnContent().asBytes();