Java使用Http远程调用

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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值