Java 通过get post 请求url

1️⃣.已获取小程序的access_token 为例,通过Get请求url

 1  import com.alibaba.fastjson.JSONObject;
 2  
 3  String wechatUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}";
 4 
 5 String appId = "xxxx";
 6 String secret = "xxxxx";
 7 
 8  String getUrl = MessageFormat.format(wechatUrl, appId, secret);
 9 
10  URL url = new URL(urlStr);
11  JSONObject jsonObject;
12 try (InputStream inputStream = url.openStream()) {
13     jsonObject = JSONObject.parseObject(IOUtils.toString(inputStream));
14 }
15 if (jsonObject.containsKey("errmsg")) {
16        throw new LobsterException("获取token,原因:" + jsonObject.getString("errmsg"));
17    }
18 return jsonObject.getString("access_token");

 2️⃣.已获取小程序码为例,通过Post请求url   也可以参考https://www.cnblogs.com/bchange/p/9156178.html

1.先设置小程序码参数

 1 package com.ieou.lobster.dto;
 2 
 3 public class MiniProgramsCode {
 4 
 5     private String scene;
 6     private String page = "pages/shop/shop";
 7     private int width = 300;
 8     private Boolean auto_color;
 9     private Object line_color;
10 
11     public String getScene() {
12         return scene;
13     }
14 
15     public void setScene(String scene) {
16         this.scene = scene;
17     }
18 
19     public String getPage() {
20         return page;
21     }
22 
23     public void setPage(String page) {
24         this.page = page;
25     }
26 
27     public int getWidth() {
28         return width;
29     }
30 
31     public void setWidth(int width) {
32         this.width = width;
33     }
34 
35     public Boolean getAuto_color() {
36         return auto_color;
37     }
38 
39     public void setAuto_color(Boolean auto_color) {
40         this.auto_color = auto_color;
41     }
42 
43     public Object getLine_color() {
44         return line_color;
45     }
46 
47     public void setLine_color(Object line_color) {
48         this.line_color = line_color;
49     }
50 }

2.对URL进行Post请求

 1 import org.apache.http.HttpResponse;
 2 import org.apache.http.client.methods.HttpPost;
 3 import org.apache.http.entity.StringEntity;
 4 import org.apache.http.impl.client.CloseableHttpClient;
 5 import org.apache.http.impl.client.HttpClientBuilder;
 6 
 7 public String getMiniProgramsQRCode(Integer restaurantId, String tableNumber, Integer tableNumberId) throws Exception {
 8 
 9         String accessToken = getMiniProgramsAccessToken();
10 
11         MiniProgramsCode miniProgramsCode = new MiniProgramsCode();
12 
13         String str = restaurantId.toString() + "*" + tableNumberId.toString() + "*" + tableNumber;
14         miniProgramsCode.setScene(str);
15         miniProgramsCode.setAuto_color(true);
16         //设置颜色
17         MiniProgramsCodeRgb miniProgramsCodeRgb = new MiniProgramsCodeRgb();
18         miniProgramsCode.setLine_color(miniProgramsCodeRgb);
19 
20         CloseableHttpClient build = HttpClientBuilder.create().build();
21         String postUrl = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken;
22         HttpPost httpPost = new HttpPost(postUrl);
23         httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
24         String jsonStr = JSONObject.toJSONString(miniProgramsCode);
25         StringEntity se = new StringEntity(jsonStr,"UTF-8");  // 中文乱码解决
26         se.setContentType("text/json");
27         se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
28         httpPost.setEntity(se);
29         HttpResponse httpResponse = build.execute(httpPost);
      if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
       String result = EntityUtils.toString(httpResponse.getEntity());// 返回json格式:
       JSONObject response = JSON.parseObject(result);
        }
30         InputStream inputStream = httpResponse.getEntity().getContent();   //返回流格式
31         }
原文地址:https://www.cnblogs.com/wang-yaz/p/9000908.html