大家好,又见面了,我是你们的朋友全栈君。
public static String sendSetHeaderGetRequestByPostForJson(String url,Map<String,String> map,Map<String,Object> psotMap/*String appUid,String listMsgIds*/ ) throws Exception{
BufferedReader buffer=null;
String result="";
try {
CloseableHttpClient httpclient = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(3000).setConnectTimeout(3000).build();//设置请求和传输超时时间
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig);
httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
httpPost.addHeader(HTTP.CONTENT_TYPE, "text/json");
httpPost.setHeader("appKey", map.get("appKey"));
httpPost.setHeader("nonce", map.get("nonce"));
httpPost.setHeader("timestamp", map.get("timestamp"));
httpPost.setHeader("signature", map.get("signature"));
JSONObject obj=JSONObject.fromObject(psotMap);
StringEntity se = new StringEntity(obj.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httpPost.setEntity(se);
CloseableHttpResponse response= httpclient.execute(httpPost);
buffer=new BufferedReader(new InputStreamReader(response.getEntity().getContent(),"utf-8"));
result =buffer.readLine();
return result;
} catch (Exception e) {
e.printStackTrace();
}finally{
buffer.close();
}
return null;
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/163273.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...