Java实现post请求(带有参数)
短信预约 -IT技能 免费直播动态提醒
package Sort.pay;import net.sf.json.JSONObject;import org.apache.commons.codec.digest.DigestUtils;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.URL;import java.net.URLConnection;import java.util.*;public class Pay { public static void main(String[] args) { String url = "http://xxxxxx";//访问路径 Random random = new Random(); Map map = new HashMap<>(); map.put("authCode", "1423009170000001"); map.put("resource", "api.hl.order.pay.unified"); map.put("requestTime", "20230129105758"); map.put("versionNo", "1"); Map params = new HashMap<>(); params.put("businessOrderNo", random.nextInt(1000000000));//202105623000002 params.put("payAmount", "90.00"); params.put("remark", "购买山核桃20斤"); params.put("merchantNo", "771389"); params.put("operatorAccount", "19884151060"); params.put("deviceNo", "402102317000001"); params.put("notifyUrl", "http://testapi.liankok.com/notify/pay"); params.put("payExpireTime", "600"); params.put("limitPay", "1"); params.put("payUrlExpireTime", "60"); params.put("subject", "山核桃"); params.put("goodsTag", "guolicheng"); params.put("goodsInfo", "{\"receiptid\":\"323323\",\"costprice\":200.00,\"goodsdetail\":[{\"goodsid\":\"123123123\",\"goodsname\":\"小石头\",\"thirdgoodsid\":\"23323453\",\"quantity\":1,\"price\":5.00}]}"); map.putAll(params);// map.put("params",params); //排序集合 map = PaySortUtil.sortByKey(map); //遍历输出拼接字符串 Set keys = map.keySet(); String str = ""; for (String key : keys) { str += key + "=" + map.get(key) + "&"; } str = str.toLowerCase() + "U7iIPg2x1k"; //md5加密字符串 String md5sr = DigestUtils.md5Hex(str); map.put("sign", md5sr); Map map3 = new HashMap<>();//存放结果 //将需要比较删除的两个集合添加 map3.putAll(map); map3.putAll(params); //for - each遍历比较且删除 for (String a : map.keySet()) { if (params.containsKey(a) || params.containsValue(map.get(a))) { map3.remove(a); } } map3.put("versionNo", "1"); map3.put("params", params); System.out.println(map3); JSONObject jsonObject=JSONObject.fromObject(map3); System.out.println(jsonObject.toString()); String ss= Pay.sendPost(url,jsonObject.toString()); System.out.println(ss); } public static String sendPost(String url,String param) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); // 打开和URL之间的连接 URLConnection conn = realUrl.openConnection(); // 设置通用的请求属性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); // 获取URLConnection对象对应的输出流 out = new PrintWriter(conn.getOutputStream()); // 发送请求参数 out.print(param); // flush输出流的缓冲 out.flush(); // 定义BufferedReader输入流来读取URL的响应 in = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.err.println("发送POST请求出现异常!" + e); e.printStackTrace(); } //使用finally块来关闭输出流、输入流 finally{ try{ if(out!=null){ out.close(); } if(in!=null){ in.close(); } } catch(IOException ex){ ex.printStackTrace(); } } return result; }}class PaySortUtil { public static Map sortByKey(Map map){ //创建一个带有比较器的TreeMap Map treeMap = new TreeMap<>(String::compareTo); //将你的map传入treeMap treeMap.putAll(map); return treeMap; }}
pom文件所需依赖,有些依赖不是必需的,这里没有删除
org.apache.httpcomponents httpcore 4.0.1 org.apache.httpcomponents httpclient 4.5.2 org.apache.httpcomponents httpmime 4.5.2 com.google.guava guava 16.0.1 org.slf4j slf4j-api 1.7.25 com.alibaba fastjson 1.2.78 org.apache.commons commons-lang3 3.12.0 commons-codec commons-codec 1.3 org.apache.maven.surefire maven-surefire-common 2.12.4 org.kie.modules org-apache-commons-httpclient 6.2.0.CR2 pom commons-beanutils commons-beanutils 1.9.3 commons-collections commons-collections 3.2.1 commons-lang commons-lang 2.6 commons-logging commons-logging 1.1.1 net.sf.ezmorph ezmorph 1.0.6 net.sf.json-lib json-lib 2.2.3 jdk15 7 7
来源地址:https://blog.csdn.net/wnagqie/article/details/128790791
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341