package com.zc.business.controller; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; 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.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; /** * @author 王思祥 * @ClassName DcWarningPush 微博推送 */ public class WeiboAuthUtil { private static final String ACCESS_TOKEN = "2.00oesadIn1MNEC0296dd00f87jmhaC"; private static final String WEIBO_API_URL = "https://api.weibo.com/2/statuses/update.json"; public static void main(String[] args) { try { String text = "这是一条通过Java和微博API推送的消息!"; // 你要推送的微博内容 postWeibo(text); } catch (Exception e) { e.printStackTrace(); } } private static void postWeibo(String status) throws Exception { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(WEIBO_API_URL); // 设置请求头,包含Content-Type httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); httpPost.setHeader("Authorization", "Bearer " + ACCESS_TOKEN); // 构造POST请求参数列表 List params = new ArrayList<>(); params.add(new BasicNameValuePair("access_token", ACCESS_TOKEN)); params.add(new BasicNameValuePair("status", status)); // 将参数列表转换为URL编码的字符串 StringEntity paramsEntity = new StringEntity(EntityUtils.toString(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8)), StandardCharsets.UTF_8); // 设置请求体内容 httpPost.setEntity(paramsEntity); // 发送请求并获取响应 HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); if (entity != null) { String responseString = EntityUtils.toString(entity, StandardCharsets.UTF_8); System.out.println("Response: " + responseString); } // 关闭HttpClient连接 httpClient.close(); } //测试调用其它的api是否可以使用,这里调用的是(通过地址编码获取地址名称) //public static void main(String[] args) { // String apiUrl = "https://api.weibo.com/2/statuses/mentions.json"; // String accessToken = "2.00oesadIn1MNEC0296dd00f87jmhaC"; // 请替换为你的微博访问令牌 // String url = "https://api.weibo.com/2/common/code_to_location.json" + "?access_token=" + accessToken + "&codes=" + "100"; // com.alibaba.fastjson.JSONObject jsonObj = null; // try { // URL urlGet = new URL(url); // HttpURLConnection http = (HttpURLConnection) urlGet.openConnection(); // http.setRequestMethod("GET"); // 必须是get方式请求 // http.setDoInput(true); // http.connect(); // InputStream is = http.getInputStream(); // int size = is.available(); // byte[] jsonBytes = new byte[size]; // is.read(jsonBytes); // String userOpenid = new String(jsonBytes, "UTF-8"); // System.out.println(userOpenid); // is.close(); // } catch (Exception e) { // e.printStackTrace(); // } // } }