1111import java .net .HttpURLConnection ;
1212import java .net .MalformedURLException ;
1313import java .net .URL ;
14+ import java .nio .charset .Charset ;
15+ import java .nio .charset .StandardCharsets ;
1416import java .util .Map ;
1517import java .util .Set ;
1618
2123 */
2224public final class HttpUtils {
2325 private static final String BASE_URL = "http://wxpusher.zjiecode.com" ;
26+ private static final String CHARSET_NAME ="UTF-8" ;
2427
2528 private HttpUtils () {
2629 }
@@ -47,11 +50,11 @@ public static Result post(Object data, String path) {
4750 urlConnection .setRequestMethod ("POST" );
4851 //设置请求属性
4952 urlConnection .setRequestProperty ("Content-Type" , "application/json" );
50- urlConnection .setRequestProperty ("Charset" , "UTF-8" );
53+ urlConnection .setRequestProperty ("Charset" , CHARSET_NAME );
5154 urlConnection .setDoOutput (true );
5255 urlConnection .connect ();
5356 OutputStream outputStream = urlConnection .getOutputStream ();
54- outputStream .write (dataStr .getBytes ());
57+ outputStream .write (dataStr .getBytes (Charset . forName ( CHARSET_NAME ) ));
5558 outputStream .flush ();
5659 return dealConnect (urlConnection );
5760 } catch (MalformedURLException e ) {
@@ -84,7 +87,7 @@ public static Result get(Map<String, Object> data, String path) {
8487 urlConnection .setUseCaches (false );
8588 urlConnection .setRequestMethod ("GET" );
8689 //设置请求属性
87- urlConnection .setRequestProperty ("Charset" , "UTF-8" );
90+ urlConnection .setRequestProperty ("Charset" , CHARSET_NAME );
8891 urlConnection .setDoOutput (true );
8992 urlConnection .connect ();
9093 return dealConnect (urlConnection );
@@ -175,7 +178,7 @@ private static String inputStream2String(InputStream inputStream) {
175178 while ((len = inputStream .read (bytes )) != -1 ) {
176179 outputStream .write (bytes , 0 , len );
177180 }
178- return new String (outputStream .toByteArray ());
181+ return new String (outputStream .toByteArray (), CHARSET_NAME );
179182 } catch (IOException e ) {
180183 e .printStackTrace ();
181184 }
0 commit comments