解密或验签失败,传参带加号“+”被转换为空格的办法

8 篇文章 0 订阅
2 篇文章 0 订阅

HTTP  post请求时  把参数 做了拼接  ”=   &  ? “  后放在url后面,导致加密参数容易出现 ”+“ 转空格,对方解密或验签失败

解决办法,不要用拼接  ”=   &  ? “ 方式传参!传json格式字符串

将HTTP  post请求 传参方式改掉  

改成  head  "Content-Type", "application/json"   json格式

Map<String,Object> map 
map.put("sign",sign.toUpperCase()); xxxx
body=JSON.toJSONString(map)

HttpUtils.doPost(url, body,head
 Map<String,Object> map= JSON.parseObject(JSON.toJSONString(request),Map.class);
        String signString= buildSignSourceDesc(map);
        //加签
        String sign = MD5Util.MD5(appSecret + signString);
        map.put("sign",sign.toUpperCase());
        String body =  JSON.toJSONString(map);
        log.info("请求报文, 参数:{}", body);
        String returnStr = HttpUtils.doPost(baisidaurl, body, null, 3, 10, "utf-8");
        log.info("调用,返回:{}", returnStr);




    /**
     * map key升序排序
     * @param requestParams
     * @return
     */
    public static String buildSignSourceDesc(Map<String, Object> requestParams) {
        requestParams.remove("responseClass");//移除不需要的字段
        if (null != requestParams && !requestParams.isEmpty()) {
            Map<String, Object> treeMap = new TreeMap<>(String::compareTo);
            treeMap.putAll(requestParams);
            StringBuilder signSrcBuilder = new StringBuilder();
            Iterator var4 = ((Map)treeMap).entrySet().iterator();
            while(var4.hasNext()) {
                Map.Entry<String, Object> entry = (Map.Entry)var4.next();
                signSrcBuilder.append(String.valueOf(entry.getKey()) ).append(String.valueOf(entry.getValue()));
            }
            return signSrcBuilder.toString();
        } else {
            return "";
        }
    }


public static String doPost(String url, String body, String[][] headers, int connectionTimeout, int readTimeout,
								String charset) {
		long startTime = System.currentTimeMillis();
		log.info(">>>doPost() #url:[{}] #body:[{}]", url, body);
		URL uri = null;
		HttpURLConnection urlConnection = null;
		try {
			uri = new URL(url);
			urlConnection = (HttpURLConnection) uri.openConnection();
			urlConnection.setRequestMethod("POST");
			urlConnection.setConnectTimeout(1000 * connectionTimeout); // 连接超时时间
			urlConnection.setReadTimeout(1000 * readTimeout); // 响应超时时间
			urlConnection.setDoOutput(true);
			urlConnection.setDoInput(true);
			urlConnection.setUseCaches(false);

			if (null == headers) {
				urlConnection.addRequestProperty("Content-Type", "application/json");
			} else {
				// 设置Http报文请求头
				for (String[] header : headers) {
					urlConnection.addRequestProperty(header[0], header[1]);
				}
			}

			// 提交Http请求参数
			urlConnection.connect();
			if (null != body) {
				log.info(">>>doPost() #url:[{}] #body:[{}]", url, body);
				urlConnection.getOutputStream().write(body.getBytes(charset));
				urlConnection.getOutputStream().flush();
				urlConnection.getOutputStream().close();
			}

			// 读取响应参数
			int responseCode = urlConnection.getResponseCode();
			if (HttpURLConnection.HTTP_OK == responseCode) {
				InputStream is = urlConnection.getInputStream();
				BufferedReader br = new BufferedReader(new InputStreamReader(is, charset));
				StringBuffer response = new StringBuffer();
				String line = br.readLine();
				while (null != line) {
					response.append(line);
					line = br.readLine();
				}
				br.close();
				is.close();
//				log.info(">>>doPost() #resp:[{}] #interval:[{}]", response, System.currentTimeMillis() - startTime);
				return response.toString();
			} else {
				log.info("#doPost() 请求失败!" + responseCode);
			}
		} catch (Exception e) {
			log.error("#doPost() 请求异常:" + e.toString(), e);
		} finally {
			if (null != urlConnection) {
				urlConnection.disconnect();
			}
		}
		return null;
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贾宝玉的贾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值