HTTP Client Post 和 Get 方式(新手使用)

//POST================================

 public JSONObject sendPostToMobileForQ(HttpServletRequest request, LlyOrder LlyOrder, String BipActivityCode){
        JSONObject jsonObject = new JSONObject();
        JSONObject json = new JSONObject();
        // 1. 创建HttpClient对象
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        // 2. 创建HttpPost对象
        HttpPost post = new HttpPost("http://antiresaletest.i139.cn/tftc_order_intf_h5/confirm/v1");
        // 3. 设置POST请求传递参数
        //随机数
        String nonce = llyUtils.getRund();
        //创建时间  格式"yyyy-MM-dd'T'HH:mm:ss'Z'".
        String created = llyUtils.getCreate();
        //密码摘要
        System.out.println("==========向移动确认订购开始============");    
        System.out.println("LlyOrder.getOrderNo()======================"+LlyOrder.getOrderNo());
        System.out.println("LlyOrder.getMobile()======================"+LlyOrder.getMobile());
        System.out.println("BipActivityCode======================"+BipActivityCode);
        System.out.println("LlyOrder.getProductCode()======================"+LlyOrder.getProductCode());
        String passworddigest = EncryptionToDecryptUtil.getBase64((EncryptionToDecryptUtil.String2SHA256(nonce+created+SECRET_KEY+(LlyOrder.getOrderNo()+LlyOrder.getMobile()+BipActivityCode+LlyOrder.getProductCode())+prodid)));
        json.put("passworddigest",passworddigest);
        json.put("nonce",EncryptionToDecryptUtil.getBase64(nonce));
        json.put("created",created);
        json.put("prodid",prodid);
        System.out.println("EncryptionToDecryptUtil.getBase64(nonce)======================"+EncryptionToDecryptUtil.getBase64(nonce));
        System.out.println("passworddigest======================"+passworddigest);
        //认证token
        post.addHeader("Authorization", json.toString());
        post.addHeader("BipCode", "BIP4B003");
        post.addHeader("ActivityCode", "T4011140");
        post.addHeader("OrderId", LlyOrder.getOrderNo());
        post.addHeader("ProductID", prodid);
        post.addHeader("content-Type", "application/json;charset=utf-8");
        JSONObject param =new JSONObject();
        param.put("OrderId",LlyOrder.getOrderNo());
        param.put("MobNum",LlyOrder.getMobile());
        param.put("BipActivityCode",String.valueOf(LlyOrder.getOrderType()));
        param.put("UserPackage",LlyOrder.getProductCode());
        param.put("UsageLimit","");
        param.put("TransParams","");
        param.put("srcTransId","");
        try {
            StringEntity se = new StringEntity(param.toString(), "utf-8");
            post.setEntity(se);
            // 4. 执行请求并处理响应
            CloseableHttpResponse responsess = httpClient.execute(post);
            HttpEntity entitys = responsess.getEntity();
            
            if (entitys != null){
                String entitystr = EntityUtils.toString(entitys);
                System.out.println("entitystr================================"+entitystr);
                jsonObject = JSONObject.fromObject(entitystr);
                System.out.println("移动返回参数================================"+jsonObject.toString());
                System.out.println("==============向移动确认结束==================");
            }
            responsess.close();
        }catch (IOException e){
            LoggerService.addErrorLog(request,e);
            e.printStackTrace();
        }finally {
            // 释放资源
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return jsonObject;
   }





//GET==================================================

String url = "http://XXXX";
JSONObject json = new JSONObject();
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse responses = null;
String resultUrl = "";//接收请求返回的结果
try {
    String urlName = url + "&"+"MobNum="+MobNum + "&UserPackage="+productCode;
    HttpGet httpget  = new HttpGet(urlName);
    //认证token
    httpget.addHeader("Authorization", json.toString());
    httpget.addHeader("BipCode", BipCode);
    httpget.addHeader("ActivityCode", ActivityCode);
    httpget.addHeader("OrderId", LlyOrder.getOrderNo());
    httpget.addHeader("ProductID", prodid);
    httpget.addHeader("content-Type", "application/json;charset=utf-8");
    //发送httpget请求
    responses = httpclient.execute(httpget);
    int result =responses.getStatusLine().getStatusCode();
    //判断
    if (result== HttpStatus.SC_OK){
        resultUrl = EntityUtils.toString(responses.getEntity(),"utf-8");
    }
} catch (Exception e) {
    e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
    // 6. 释放资源
    try {
        responses.close();
        httpclient.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值