public static String getSuiteTokenToJson2(String ticket){
String returnStr = HttpRequestUtil.sendPost("https://qyapi.weixin.qq.com/cgi-bin/service/get_suite_token",
"suite_id="+WxCfg.WX_COMP_SECRET+"&suite_secret="+WxCfg.WX_COMP_SECRET+"&suite_ticket="+ticket);
return returnStr;
}
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
StringBuilder result = new StringBuilder();
try {
log.info("\n\n=====================sendPost 请求报文:========================="+
"\n>>url:"+url+
"\n>>param:"+param+
"\n===================================================================\n");
URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setRequestProperty("Accept-Charset", "UTF-8");
conn.setRequestProperty("contentType", "UTF-8");
conn.setDoOutput(true);
conn.setDoInput(true);
out = new PrintWriter(conn.getOutputStream());
out.print(param);
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
} catch (Exception e) {
og.error("发送 POST 请求出现异常!" + e.getMessage());
result = new StringBuilder("{\"resCode\":\"1\",\"errCode\":\"1001\",\"resData\":\"\"}");
e.printStackTrace();
}
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
log.info("\n\n=====================sendPost 响应报文:========================="+
"\n>>url:"+url+
"\n>>报文体:"+result.toString()+
"\n===================================================================\n");
return result.toString();
}
问题已解决,param封装形式错误
public static String getSuiteTokenToJson2(String ticket){ JSONObject json = new JSONObject(); json.put("suite_id", WxCfg.WX_COMP_SUITEID);//应用id json.put("suite_secret", WxCfg.WX_COMP_SECRET);//应用secret json.put("suite_ticket", ticket);//企业微信后台推送的ticket String returnStr = HttpRequestUtil.sendPost("https://qyapi.weixin.qq.com/cgi-bin/service/get_suite_token",json.toString()); return returnStr; }