邯郸建站公司/国内新闻最新5条
和第三方对接时,对方的接口url路径参数拼接中需要加上xml的参数,当时接触地特别少,就以为直接使用<>在路径后边拼接就行,然后就报错了
完整的拼错的url路径是
到xml参数的位置就报错了
最开始的时候:拼接这个xml参数是我是直接这样写的,然后直接拼到了对应的data属性名后边
/*** 获取业务xml参数** @return*/public StringBuilder getXmlBusinessParam() {StringBuilder builder = new StringBuilder();builder.append("<request>");builder.append("<count>").append("20").append("</count>");builder.append("<start>").append("0").append("</start>");builder.append("</request>");return builder;}
后来查了相关的博客,说的是url路径拼接时一些特殊的字符要进行转换:
最终把xml参数的拼接另外写成了一个方法,写成了这个样子
/*** 获取业务xml参数,拼接接口地址使用** @return*/public StringBuilder getXmlBusinessParamURL() {StringBuilder builder = new StringBuilder();builder.append("%3C").append("request").append("%3E");builder.append("%3C").append("count").append("%3E").append("20").append("%3C").append("%2F").append("count").append("%3E");builder.append("%3C").append("start").append("%3E").append("0").append("%3C").append("%2F").append("start").append("%3E");builder.append("%3C").append("%2F").append("request").append("%3E");return builder;}
运行之后的结果是
我的http请求路径代码是用的httpPost,大概如下
String sign = shYiBusinessConfig.getSign();if (!StringUtils.hasText(sign)) {throw new BusinessException(AnwserCode.APPLICATION_EXCEPTIONS, "胜意商旅单点登录获取接口授权密钥失败");}/*** 总公司编号*/String companyId = shYiBusinessConfig.getCompanyId();/*** 请求参数封装成xml*/StringBuilder xmlParam = getXmlBusinessParam();log.info("单点登录获取所有请求参数封装成xml出参,xmlParam:{}", xmlParam);/*** 获取签名*/String sinature = ShengYiUrlUtils.generateSinature(companyId, xmlParam.toString(), shYiBusinessConfig.getSsoSign());log.info("单点登录获取签名出参,sinature:{}", sinature);if (!StringUtils.hasText(sinature)) {throw new BusinessException(AnwserCode.APPLICATION_EXCEPTIONS, "胜意商旅单点登录获取签名失败");}StringBuilder xmlBusinessParam = getXmlBusinessParamURL();log.info("单点登录获取业务参数xml出参,xmlBusinessParam:{}", xmlBusinessParam);String loginurl = shYiBusinessConfig.getLoginurl();StringBuilder builder = new StringBuilder();builder.append(loginurl);builder.append("responseType=2&");builder.append("version=1&");builder.append("sign=").append(sinature).append("&");builder.append("data=").append(xmlBusinessParam.toString()).append("&");builder.append("account=").append(sjh).append("&");builder.append(deviceParamName).append("=").append(sjh).append("&");builder.append("channel=").append(channel).append("&");builder.append("compid=").append(companyId).append("&");builder.append("service=COMM_B2C_equipmentRegistration");builder.append("&websiteCode=C");log.info("单点登录请求参数值,builder:{}", builder.toString());HttpPost httpPost = new HttpPost(builder.toString());CloseableHttpResponse response = null;try {response = httpClient.execute(httpPost);HttpEntity entity = response.getEntity();byte[] bytes = EntityUtils.toByteArray(entity);String s = new String(bytes, "utf-8");log.info("胜意商旅单点登录返回值: {}", s);JSONObject data = JSONObject.parseObject(s);JSONObject res = (JSONObject) data.get("res");Integer sts = (Integer) res.get("sts");String erc = (String) res.get("erc");if (sts != 1 || !"SUCCESS".equals(erc.trim())) {log.error("胜意商旅单点登录调用失败", s);throw new BusinessException(new AnwserCode(10001, "胜意商旅单点登录失败"));}/*** 单点登录成功, 缓存token*/this.shYiDao.setSsoToken(sjh);} catch (IOException exp) {log.error("胜意商旅单点登录失败", exp);throw new BusinessException(new AnwserCode(10001, "胜意商旅单点登录失败"));} finally {try {if (httpClient != null) {httpClient.close();httpClient = null;}if (response != null) {response.close();response = null;}} catch (IOException exp) {exp.printStackTrace();}}
推荐博客:
https://blog.csdn.net/liuchuanhong1/article/details/70161149
https://blog.csdn.net/zsj777/article/details/80654567