HttpClient4在HTTP类里原先定义了一系列编码集常量UTF-8,UTF-16等,但从4.2版本开始,这些常量被标识了deprecated标志。java
/** * @deprecated (4.2) */ @Deprecated public static final String UTF_8 = "UTF-8"; /** * @deprecated (4.2) */ @Deprecated public static final String UTF_16 = "UTF-16"; /** * @deprecated (4.2) */ @Deprecated public static final String US_ASCII = "US-ASCII"; /** * @deprecated (4.2) */ @Deprecated public static final String ASCII = "ASCII";
这是由于从JDK7起,引入了StandardCharsets类,标识了这些编码集常量ide
/** * Constant definitions for the standard {@link Charset Charsets}. These * charsets are guaranteed to be available on every implementation of the Java * platform. * * @see <a href="Charset#standard">Standard Charsets</a> * @since 1.7 */ public final class StandardCharsets { private StandardCharsets() { throw new AssertionError("No java.nio.charset.StandardCharsets instances for you!"); } /** * Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the * Unicode character set */ public static final Charset US_ASCII = Charset.forName("US-ASCII"); /** * ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1 */ public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1"); /** * Eight-bit UCS Transformation Format */ public static final Charset UTF_8 = Charset.forName("UTF-8"); /** * Sixteen-bit UCS Transformation Format, big-endian byte order */ public static final Charset UTF_16BE = Charset.forName("UTF-16BE"); /** * Sixteen-bit UCS Transformation Format, little-endian byte order */ public static final Charset UTF_16LE = Charset.forName("UTF-16LE"); /** * Sixteen-bit UCS Transformation Format, byte order identified by an * optional byte-order mark */ public static final Charset UTF_16 = Charset.forName("UTF-16"); }
所以之后咱们使用HttpClient 4发送POST请求,设定编码集时,不须要再使用HTTP类中的编码集常量,直接使用StandardCharsets类中的编码集常量便可编码