Java使用Http远程调用
↓↓
↓↓
↓↓
正文
public Object backstagePushMessageT(@RequestParam(value = "pushObjecType") String pushObjecType,
@RequestParam(value = "toUserIds", required = false) String toUserIds,
@RequestParam(value = "groupId", required = false) String groupId,
@RequestParam(value = "pushMessage") String pushMessage,
HttpServletRequest request) {
HttpClient client = HttpClients.createDefault();
String url = null;
String appKey = "appKey";
//签名
String Signature = "1d938e487c41f523dfscb3sd263be896";
//参数
List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
JSONObject jsonObject = new JSONObject();
url = URL + "/api/im/httpMessage/windowNtf";
nvps.add(new BasicNameValuePair("groupId", groupId));
nvps.add(new BasicNameValuePair("msg", pushMessage));
HttpPost post = HttpPostUtil.getHttpPost(url, appKey, Signature);
try {
post.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
HttpResponse res = client.execute(post);
if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(res.getEntity());// 返回json格式:
System.out.println("result;" + result);
jsonObject = JSONObject.parseObject(result);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return jsonObject;
}
HttpPostUtil :
public class HttpPostUtil {
//单聊窗口通知
private static final String
URL = "127.0.0.1:8080";
//网易云信分配的账号,请替换你在管理后台应用下申请的Appkey
private static final String
APP_KEY = "appkey";
//随机数
private static final String Nonce = "123456";
public static HttpPost getHttpPost(String url,String appKey,String signature) {
HttpPost post = new HttpPost(url);
post.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
post.addHeader("App-Key", appKey); //先默认-------------------
post.addHeader("Nonce", Nonce);
post.addHeader("Timestamp", "1504320656000");
post.addHeader("Signature", signature);
return post;
}
}