Java Http GET POST发送请求

た 入场券 2022-08-05 02:46 424阅读 0赞

Java Http GET POST发送请求

本文写了1个java 发送GET请求以及2个java 发送POST请求,新手,不喜勿喷!

背景:

这是一个使用魔宝支付的demo,首先需要移动端提交商城订单,请求平台签名接口进行签名并获取支付所需要的要素,对支付公司返回的信息验签后返回移动端这些要素,移动端启动支付公司SDK进行支付交易,后续还有接收交易结果通知消息。

说明

  • GET核心:CloseableHttpClient和CloseableHttpResponse,HttpGet
  • POST核心:HttpURLConnection和HttpPost

功能步骤说明

  1. 第一步:GET请求从服务器签名接口进行签名操作;
  2. 第二步:GET请求至魔宝支付进行获取支付所需要素;
  3. 第三步:POST验签魔宝返回的签名信息,防篡改

静态变量

  • 订单信息:private static String tranData = “XML订单信息”;
  • 签名接口:private static String signUrl = “http://10.3.30.17:8099/chpay/v1/pay/mobao/sign“;
  • 验签接口private static String verifyUrl = “http://10.3.30.17:8099/chpay/v1/pay/mobao/signature/verify“;
  • 魔宝接口:private static String mobaoUrl = “http://182.148.123.7:8132/service“;

第一步:签名

  • 在交易之前需要使用支付方提供的证书进行签名交易订单
  1. --------------------
  2. /** * 签名订单信息 */
  3. public static JSONObject testHttpGet(String data) throws UnsupportedEncodingException {
  4. CloseableHttpClient httpClient = HttpClients.custom().disableContentCompression().build();
  5. StringBuffer qUrl = new StringBuffer(signUrl);
  6. qUrl.append("?tranData=" + URLEncoder.encode(data, "UTF-8"));
  7. HttpGet httpget = new HttpGet(new String(qUrl));
  8. try {
  9. CloseableHttpResponse httpResponse = httpClient.execute(httpget);
  10. HttpEntity entity = httpResponse.getEntity();
  11. //返回内容
  12. String res = EntityUtils.toString(entity, "UTF-8");
  13. return JSONObject.parseObject(res);
  14. } catch (IOException e) {
  15. e.printStackTrace();
  16. }
  17. return null;
  18. }

第二步:获取支付要素

  • 使用支付方规定的格式,签名后请求支付公司接口得到支付所需要的要素,支付公司返回要素以及签名信息

    public static String getPayElements(JSONObject json) throws UnsupportedEncodingException {

    1. if (json.containsKey("message") && json.containsKey("signature")) {
    2. String message = json.getString("message");
    3. String signature = json.getString("signature");
    4. String param = "message=" + message + "&signature=" + signature;
    5. String paramEncode = URLEncoder.encode(param, "UTF-8");
    6. CloseableHttpClient httpClient = HttpClients.custom().disableContentCompression().build();
    7. StringBuffer qUrl = new StringBuffer(mobaoUrl);
    8. qUrl.append("?message=" + URLEncoder.encode(message, "UTF-8"));
    9. qUrl.append("&signature=" + URLEncoder.encode(signature, "UTF-8"));
    10. HttpGet httpget = new HttpGet(new String(qUrl));
    11. try {
    12. CloseableHttpResponse httpResponse = httpClient.execute(httpget);
    13. HttpEntity entity = httpResponse.getEntity();
    14. //返回内容
    15. String res = EntityUtils.toString(entity, "UTF-8");
    16. return res;
    17. } catch (IOException e) {
    18. e.printStackTrace();
    19. }
    20. }
    21. return null;
    22. }

第三步:验签

  • 获取支付要素时支付公司返回签名信息,需要进行验签,防止信息被篡改。

    public static JSONObject verifySign(JSONObject jsonObject) {

    1. if (jsonObject.containsKey("message") && jsonObject.containsKey("signature")) {
    2. String message = jsonObject.getString("message");
    3. String signature = jsonObject.getString("signature");
    4. try {
    5. //创建连接
    6. URL url = new URL(verifyUrl);
    7. HttpURLConnection connection = (HttpURLConnection) url
    8. .openConnection();
    9. connection.setDoOutput(true);
    10. connection.setDoInput(true);
    11. connection.setRequestMethod("POST");
    12. connection.setUseCaches(false);
    13. connection.setInstanceFollowRedirects(true);

    // connection.setRequestProperty(“Accept”, “application/json”); // 设置接收数据的格式

    1. connection.setRequestProperty("Content-Type", "application/json");
    2. connection.connect();
    3. //POST请求
    4. DataOutputStream out = new DataOutputStream(connection.getOutputStream());
    5. JSONObject obj = new JSONObject();
    6. obj.put("message", message);
    7. obj.put("signature", signature);
    8. out.writeBytes(obj.toString());
    9. out.flush();
    10. out.close();
    11. //读取响应
    12. BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    13. String lines;
    14. StringBuffer sb = new StringBuffer("");
    15. while ((lines = reader.readLine()) != null) {
    16. lines = new String(lines.getBytes(), "utf-8");
    17. sb.append(lines);
    18. }
    19. reader.close();
    20. // 断开连接
    21. connection.disconnect();
    22. return JSONObject.parseObject(sb.toString());
    23. } catch (MalformedURLException e) {
    24. // TODO Auto-generated catch block
    25. e.printStackTrace();
    26. } catch (UnsupportedEncodingException e) {
    27. // TODO Auto-generated catch block
    28. e.printStackTrace();
    29. } catch (IOException e) {
    30. // TODO Auto-generated catch block
    31. e.printStackTrace();
    32. }
    33. }
    34. return null;
    35. }

附:另外一个POST请求方式

  1. public static String post(String json, String url) throws Exception{
  2. StringEntity entity = new StringEntity(json, "utf-8");
  3. entity.setContentType("application/json");
  4. DefaultHttpClient client = new DefaultHttpClient();
  5. HttpPost httpPost = new HttpPost(url);
  6. httpPost.setEntity(entity);
  7. HttpResponse response = client.execute(httpPost);
  8. InputStream inputStream = response.getEntity().getContent();
  9. StringBuffer buffer = new StringBuffer();
  10. InputStreamReader inputReader = new InputStreamReader(inputStream);
  11. BufferedReader bufferReader = new BufferedReader(inputReader);
  12. String str;
  13. while ((str = bufferReader.readLine()) != null) {
  14. buffer.append(str);
  15. }
  16. bufferReader.close();
  17. String jsonOut = buffer.toString();
  18. return jsonOut;
  19. }

我的联系方式:
- Q Q:1250052380
- 邮箱:1250052380@qq.com

发表评论

表情:
评论列表 (有 0 条评论,424人围观)

还没有评论,来说两句吧...

相关阅读