httpClient发送请求

以下是自己在工作中写的代码

/**
	 * 发送请求
	 * @param url
	 * @return
	 */
	private HttpResponse sendRequest(String url,List<NameValuePair> params) throws Exception{
		HttpResponse response = null;
		try{
			HttpClient httpClient = HttpClients.createDefault();
	        HttpPost httpPost = new HttpPost(url);
	        httpPost.setEntity(new UrlEncodedFormEntity(params, Encoding));
	        response = httpClient.execute(httpPost);
		}catch(Exception e){
			MyLog.error("请求失败! url = " + url + "请求参数:" + JSONObject.toJSONString(params));
			throw new Exception("请求失败! url = " + url);
		}
        return response;
	}
	
	/**
	 * 无短信验证代扣签约
	 * @param vo
	 */
	public ShanqbReturnInfo noSmsSign(ShanqbCustVo vo) throws Exception{
		//数据校验
		ShanqbUtil.dataCheck(vo, InterfaceType.SIGN);
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		String returnStr = "";
		ShanqbReturnInfo returnInfo = new ShanqbReturnInfo();
		String mercId = ResourceUtil.getPropertyValue("sx.mercId");//商户编号
		String interKey = ResourceUtil.getPropertyValue("sx.key");//商户秘钥
		String url = ResourceUtil.getPropertyValue("sx.sign.url");//无短信验证签约测试地址
		String interfaceName = ResourceUtil.getPropertyValue("sx.sign.name");//无短信签约测试接口名称


        String signKey = "";// 加密密钥必须为32位, 如果商户接口密钥长度大于32位则直接截取,如果不够32位则右补0
        signKey = ShanqbUtil.getSignKey(interKey);
		
		String cardNo = ShanqbUtil.enMode(signKey,vo.getCardNo());
		String cardCVV2 = ShanqbUtil.enMode(signKey,vo.getCardCVV2());
		String cardExpDate = ShanqbUtil.enMode(signKey,vo.getCardExpDate());
		String cardName = ShanqbUtil.enMode(signKey,vo.getCardName());
		String idNo = ShanqbUtil.enMode(signKey,vo.getIdNo());
		String mobileNo = vo.getMobileNo();
		String requestId = vo.getRequestId();
		String idType = vo.getIdType();
		String rmk = vo.getRmk();
		String charset = Encoding;
		String signType = this.signType;
		String version = this.version;
		
		String signStr = charset + mercId + requestId + interfaceName + version + signType + mobileNo + cardNo + cardCVV2 + cardExpDate + cardName + idType + idNo + rmk;
		MyLog.info("待签名字符串" + signStr);
		
		String hmac = ShanqbUtil.sign(signStr, interKey);
		MyLog.info("签名字符串" + hmac);
		
		params.add(new BasicNameValuePair("charset", charset));
        params.add(new BasicNameValuePair("mercId", mercId));
        params.add(new BasicNameValuePair("requestId", requestId));
        params.add(new BasicNameValuePair("interfaceName", interfaceName));
        params.add(new BasicNameValuePair("version", version));
        params.add(new BasicNameValuePair("signType", signType));
		params.add(new BasicNameValuePair("hmac", hmac));
		params.add(new BasicNameValuePair("mobileNo", mobileNo));
		params.add(new BasicNameValuePair("cardNo", cardNo));
		params.add(new BasicNameValuePair("cardCVV2", cardCVV2));
		params.add(new BasicNameValuePair("cardExpDate", cardExpDate));
		params.add(new BasicNameValuePair("cardName", cardName));
		params.add(new BasicNameValuePair("idType", idType));
		params.add(new BasicNameValuePair("idNo", idNo));
		params.add(new BasicNameValuePair("rmk", rmk));

		MyLog.info("发送URL:" + url);
		
		 // 发送请求
        HttpResponse response = this.sendRequest(url, params);
        JSONObject returnObj = null;
        String returnCode = "";
        String message = "";
        String agrNo = "";
        if (response.getStatusLine().getStatusCode() == 200) {// 如果状态码为200,就是正常返回
            returnStr = EntityUtils.toString(response.getEntity());
            returnObj = JSONObject.parseObject(returnStr);
            
            System.out.println("返回报文:" + returnStr);// 返回数据为标准格式的json串
            MyLog.info("返回报文:" + returnStr);
            
            returnCode = returnObj.getString("returnCode");
            message = returnObj.getString("message");
            agrNo = returnObj.getString("agrNo");
            returnInfo.setReturnCode(returnCode);
            returnInfo.setMessage(message);
            returnInfo.setAgrNo(agrNo);
        }else{
        	//未正常返回
        	returnStr = EntityUtils.toString(response.getEntity());
        	returnObj = JSONObject.parseObject(returnStr);
        	
        	System.out.println("返回报文:" + returnStr);
        	MyLog.warn("未正常返回" + returnStr);
        	returnInfo.setReturnCode("0000");
        	returnInfo.setMessage("未正常返回");
        }
        //记录流水到数据库
  		SxOperationRecord record = new SxOperationRecord(vo.getRequestId(),interfaceName,returnObj.toJSONString(),JSONObject.toJSONString(params),url,vo.getOrderId());
  		this.commDao.save(record);
		
		return returnInfo;
	}



 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值