微信小程序开发登录代码开发微信开发者工具下载地址与更新日志 https://developers.weixin.qq.com/miniprogram/dev/devtools/download.html
利用HttpClient把网络请求拿出来
HttpClient 介绍
导入Maven依赖
但是我们项目里导入了阿里云Oss的依赖 所以不导入此依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
通过HttpClient发送GET方式请求
/**
* 通过HttpClient发送GET方式请求
*/
@Test
public void testGet() throws Exception{
// 创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建Http请求对象
HttpGet httpGet = new HttpGet("http://localhost:8080/user/shop/status");
// 发送请求,接收响应结果
CloseableHttpResponse response = httpClient.execute(httpGet);
// 获取服务端返回的状态码
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("服务端返回的状态码为:" +statusCode);
HttpEntity entity = response.getEntity();
String body = EntityUtils.toString(entity);
System.out.println("服务端返回的状态码为: " + body);
// 释放资源
response.close();
httpClient.close();
}
通过HttpClient发送POST方式请求
/**
* 通过HttpClient发送POST方式请求
*/
@Test
public void testPost() throws Exception{
// 创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建Http请求对象
HttpPost httpPost = new HttpPost("http://localhost:8080/admin/employee/login");
JSONObject jsonObject = new JSONObject();
jsonObject.put("username", "admin");
jsonObject.put("password", "123456");
StringEntity entity = new StringEntity(jsonObject.toString());
// 指定请求编码方式
entity.setContentEncoding("UTF-8");
// 指定请求参数的格式是json格式
entity.setContentType("application/json");
httpPost.setEntity(entity);
// 发送请求
CloseableHttpResponse response = httpClient.execute(httpPost);
// 解析响应结果
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("服务端返回的响应码为:" + statusCode);
HttpEntity entity1 = response.getEntity();
String body = EntityUtils.toString(entity1);
System.out.println("服务端返回的响应结果为:" + body);
// 释放资源
response.close();
httpClient.close();
}
配置yml文件
微信登录代码逻辑开发
在小程序文档里可以看到登录流程
UserController 的代码