Java 通过get post 请求url

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

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

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

1.先设置小程序码参数

1 1 package com.ieou.lobster.dto; 2 2 3 3 public class MiniProgramsCode { 4 4 5 5 private String scene; 6 6 private String page = "pages/shop/shop"; 7 7 private int width = 300; 8 8 private Boolean auto_color; 9 9 private Object line_color; 1010 1111 public String getScene() { 1212 return scene; 1313 } 1414 1515 public void setScene(String scene) { 1616 this.scene = scene; 1717 } 1818 1919 public String getPage() { 2020 return page; 2121 } 2222 2323 public void setPage(String page) { 2424 this.page = page; 2525 } 2626 2727 public int getWidth() { 2828 return width; 2929 } 3030 3131 public void setWidth(int width) { 3232 this.width = width; 3333 } 3434 3535 public Boolean getAuto_color() { 3636 return auto_color; 3737 } 3838 3939 public void setAuto_color(Boolean auto_color) { 4040 this.auto_color = auto_color; 4141 } 4242 4343 public Object getLine_color() { 4444 return line_color; 4545 } 4646 4747 public void setLine_color(Object line_color) { 4848 this.line_color = line_color; 4949 } 5050 }

2.对URL进行Post请求

1 1 import org.apache.http.HttpResponse; 2 2 import org.apache.http.client.methods.HttpPost; 3 3 import org.apache.http.entity.StringEntity; 4 4 import org.apache.http.impl.client.CloseableHttpClient; 5 5 import org.apache.http.impl.client.HttpClientBuilder; 6 6 7 7 public String getMiniProgramsQRCode(Integer restaurantId, String tableNumber, Integer tableNumberId) throws Exception { 8 8 9 9 String accessToken = getMiniProgramsAccessToken(); 1010 1111 MiniProgramsCode miniProgramsCode = new MiniProgramsCode(); 1212 1313 String str = restaurantId.toString() + "*" + tableNumberId.toString() + "*" + tableNumber; 1414 miniProgramsCode.setScene(str); 1515 miniProgramsCode.setAuto_color(true); 1616 //设置颜色 1717 MiniProgramsCodeRgb miniProgramsCodeRgb = new MiniProgramsCodeRgb(); 1818 miniProgramsCode.setLine_color(miniProgramsCodeRgb); 1919 2020 CloseableHttpClient build = HttpClientBuilder.create().build(); 2121 String postUrl = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken; 2222 HttpPost httpPost = new HttpPost(postUrl); 2323 httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json"); 2424 String jsonStr = JSONObject.toJSONString(miniProgramsCode); 2525 StringEntity se = new StringEntity(jsonStr,"UTF-8"); // 中文乱码解决 2626 se.setContentType("text/json"); 2727 se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 2828 httpPost.setEntity(se); 2929 HttpResponse httpResponse = build.execute(httpPost); 30 31      if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {        String result = EntityUtils.toString(httpResponse.getEntity());// 返回json格式:        JSONObject response = JSON.parseObject(result);        } 32 3330 InputStream inputStream = httpResponse.getEntity().getContent(); //返回流格式 3431 }
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Java日期时间API系列31

  时间戳是指格林威治时间1970年01月01日00时00分00秒起至现在的总毫秒数,是所有时间的基础,其他时间可以通过时间戳转换得到。Java中本来已经有相关获取时间戳的方法,Java8后增加新的类Instant等专用于处理时间戳问题。 1获取时间戳的方法和性能对比1.1获取时间戳方法Java8以前