http发送post请求中文乱码问题

 1 package com.lwl.util;
 2 
 3 import sun.misc.BASE64Encoder;
 4 
 5 import java.io.*;
 6 import java.net.HttpURLConnection;
 7 import java.net.URL;
 8 import java.net.URLConnection;
 9 import java.nio.charset.StandardCharsets;
10 
11 /**
12  * post请求
13  *
14  * @author liuwenlong
15  * @create 2021-11-30 22:25:00
16  */
17 @SuppressWarnings("all")
18 public class SendPost {
19 
20     /**
21      * Post请求一个地址
22      *
23      * @param URL         请求地址
24      * @param requestBody 请求的body
25      * @return
26      */
27     public String doPost(String URL, String requestBody) {
28         OutputStreamWriter out = null;
29         BufferedReader in = null;
30         StringBuilder result = new StringBuilder();
31         HttpURLConnection conn = null;
32         String username = "账号";
33         String password = "密码";
34         String input = username + ":" + password;
35         try {
36             java.net.URL url = new URL(URL);
37             conn = (HttpURLConnection) url.openConnection();
38             BASE64Encoder base = new BASE64Encoder();
39             String encodedPassword = base.encode(input.getBytes("UTF-8"));
40             System.out.println("加密后的密码:" + encodedPassword);
41             //将加密的账号密码放到请求头里,这里注意Basic后面要加空格
42             conn.setRequestProperty("Authorization", "Basic " + encodedPassword);
43             conn.setRequestMethod("POST");
44             //发送POST请求必须设置为true
45             conn.setDoOutput(true);
46             conn.setDoInput(true);
47             //设置连接超时时间和读取超时时间
48             conn.setConnectTimeout(3000);
49             conn.setReadTimeout(3000);
50             conn.setRequestProperty("Content-Type","application/json;charset=UTF-8");
51             conn.setRequestProperty("Accept", "application/json");
52             //获取输出流,写入请求的json报文
53             out = new OutputStreamWriter(conn.getOutputStream(),"utf-8");
54             System.out.println(requestBody);
55 
56             out.write(requestBody); //获取请求的body,
57             out.flush();
58             out.close();
59             //取得输入流,并使用Reader读取
60             if (200 == conn.getResponseCode()) {
61                 in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
62                 String line;
63                 while ((line = in.readLine()) != null) {
64                     result.append(line);
65                     System.out.println(line);
66                 }
67             } else {
68                 System.out.println("ResponseCode is an error code:" + conn.getResponseCode());
69             }
70         } catch (Exception e) {
71             e.printStackTrace();
72         } finally {
73             try {
74                 if (out != null) {
75                     out.close();
76                 }
77                 if (in != null) {
78                     in.close();
79                 }
80             } catch (IOException ioe) {
81                 ioe.printStackTrace();
82             }
83         }
84         return result.toString();
85     }
86 
87 }

注意,OutputStreamWriter 后面要添加 utf-8

posted @ 2021-12-03 01:33  勤快的懒羊羊  阅读(1279)  评论(0编辑  收藏  举报