public static String sendPostV2(String postUrl,String params,String headers){
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
//建立一個連接
try {
HttpPost httpPost = new HttpPost(postUrl);
//這個防止中文???,很重要,很重要,很重要
httpPost.setEntity(new StringEntity(params, Charset.forName("UTF-8")));
//設置頭信息
JSONObject jsonObject = JSONObject.parseObject(headers);
Set<String> keys = jsonObject.keySet();
for (String key:keys){
httpPost.setHeader(key,jsonObject.getString(key));
}
//發送
HttpResponse httpResponse = closeableHttpClient.execute(httpPost);
int returnCode = httpResponse.getStatusLine().getStatusCode();
if (returnCode== HttpStatus.SC_OK){
InputStream inputStream = httpResponse.getEntity().getContent();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuffer stringBuffer = new StringBuffer();
String line = "";
while ((line = bufferedReader.readLine())!=null){
stringBuffer.append(line);
}
return stringBuffer.toString();
}
}catch (IOException e){
e.printStackTrace();
JSONObject jsonObject = new JSONObject();
jsonObject.put("code","404");
jsonObject.put("result","no result");
return jsonObject.toJSONString();
}
JSONObject jsonObject = new JSONObject();
jsonObject.put("code","404");
jsonObject.put("result","no result");
return jsonObject.toJSONString();
}
解決一次模擬post請求的時候,出現中文???的錯誤
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.