大家好,隔了快半年了(原因是因为博主在工作,顾不上了,但是看到每天都有人从我这学到有用的知识,感觉好开心啊啊!!!),终于发文了,激动,话不多说,直接切入正题:
博主在工作中遇到需要把公众号的内容弄到小程序上展示,中间的存储媒介是数据库,换句话说,就是需要先调用接口,再把数据存储到数据库里,最后给到软件去使用。
本次使用到的maven依赖是:
<dependency>
<?用来模拟浏览器发送http请求的一个工具?>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<?json解析工具?>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.48</version>
</dependency>
主要是需要access_token,而access_token的获取是要appid跟sercet,具体作用参考公众号官方开发文档
https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Global_Return_Code.html
你们使用的话,需要在代码中换appid跟sercet的值
如何知道自己公众号的appid跟sercet的值,登陆网页公众号即可知道,切记需要把你运行代码的机器IP放进白名单里,如何知道自己机器IP,在不设置白名单运行程序时,他会报错,在报错里告诉你的ip,然后把ip设置到白名单,再重新运行即可
具体代码如下:
package HttpWechat;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;
/**
* author:haigui
* data:2022/3/21
* desc:
**/
public class HttpMain {
// http请求类
private static class HttpRequest {
private static String html;
private static HttpGet httpGet;
private static HttpPost httpPost;
private static CloseableHttpResponse response;
private static String httpGet() throws IOException {
// qppid
String appid_value = appid;
// secret
String secret_value = secret;
//1. 创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//2,url
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
+appid_value+"&secret="+secret_value;
//3.发起Get请求
httpGet = new HttpGet(url);
//4.提交请求
response = httpClient.execute(httpGet);
//5.输出内容
if (response.getStatusLine().getStatusCode() == 200) { //200表示响应成功
html = EntityUtils.toString(response.getEntity(), "UTF-8");
return html;
}
//6.关闭资源
httpClient.close();
response.close();
return "请求失败!!!";
}
private static String httpPost(String url) throws IOException {
//1. 创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//2.post请求
httpPost = new HttpPost(url);
//3.json参数传入
JSONObject param_json = new JSONObject();
param_json.put("offset", "0");
param_json.put("count", "20");
param_json.put("no_content", "0");
// 使用URL实体转换工具
httpPost.setEntity(new StringEntity(param_json.toString()));
//4.执行Post请求
response = httpClient.execute(httpPost);
//5.输出内容
if (response.getStatusLine().getStatusCode() == 200) { //200表示响应成功
html = EntityUtils.toString(response.getEntity(), "UTF-8");
return html;
}
//5.关闭资源
httpClient.close();
response.close();
return "请求失败!!!";
}
}
public static void main(String[] args) throws IOException, URISyntaxException {
//获取access_token的json
String access_token_json = HttpRequest.httpGet();
//提取出access_token
JSONObject jsonObject = JSONObject.parseObject(access_token_json);
String access_token = jsonObject.getString("access_token");
// 提交post请求
String post = HttpRequest.httpPost("https://api.weixin.qq.com/cgi-bin/freepublish/batchget?access_token="+access_token);
//输出返回数据
System.out.println(post);
}
}
感谢观看!!