以下是自己在工作中写的代码
/**
* 发送请求
* @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;
}