日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java代码 微信公众号推送(代码详细)

發布時間:2024/1/1 java 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java代码 微信公众号推送(代码详细) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

用的是idea,?需要修改的地方在于這些地方改了

?

?

然后吧賬號密碼填寫進去就好了? 運行類是? ?Yi? ?這個類

非常簡單?祝大家成功 ,不會的關注私聊??

微信公眾號測試注冊地址:微信公眾平臺 (qq.com)

?我用的模板是這個只有一個數據 ,當然這個可以自己添加

?做出來的效果當然上面那個要添加什么只需要在這個類里面添加對應的數據值:

?

?

?

?需要導入的jar包

<dependencies><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpmime</artifactId><version>4.3.6</version></dependency><!-- https://mvnrepository.com/artifact/org.jsoup/jsoup --><dependency><groupId>org.jsoup</groupId><artifactId>jsoup</artifactId><version>1.11.2</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.4</version></dependency><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.4.0</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.47</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.3.6</version></dependency><dependency><groupId>org.thymeleaf</groupId><artifactId>thymeleaf</artifactId><version>3.0.15.RELEASE</version></dependency></dependencies>

public class ABC {private int abc=1;private int abc1=1;private int abc2=1;public ABC() {}public int getAbc() {return abc;}public void setAbc(int abc) {this.abc = abc;}public int getAbc1() {return abc1;}public void setAbc1(int abc1) {this.abc1 = abc1;}public int getAbc2() {return abc2;}public void setAbc2(int abc2) {this.abc2 = abc2;} }

import java.awt.*; import java.util.Random;public class Close {public static void main(String[] args) {for(int i=0;i<=10;i++){System.out.println(close());}}public static String close(){//紅色String red;//綠色String green;//藍色String blue;//生成隨機對象Random random = new Random();//生成紅色顏色代碼red = Integer.toHexString(random.nextInt(256)).toUpperCase();//生成綠色顏色代碼green = Integer.toHexString(random.nextInt(256)).toUpperCase();//生成藍色顏色代碼blue = Integer.toHexString(random.nextInt(256)).toUpperCase();//判斷紅色代碼的位數red = red.length()==1 ? "0" + red : red ;//判斷綠色代碼的位數green = green.length()==1 ? "0" + green : green ;//判斷藍色代碼的位數blue = blue.length()==1 ? "0" + blue : blue ;//生成十六進制顏色值String color = "#"+red+green+blue;System.out.println(color);return color;}} import java.io.*; import java.net.HttpURLConnection; import java.net.URL;public class Main {public static String getHtmlByPost(String urlString,String postBody) throws IOException {byte[] xmlData = postBody.getBytes();//接收想要連接網址的地址URL url=new URL(urlString);//響應StringBuilder responseBuilder=new StringBuilder();//讀取信息為文本信息,所以用bufferReaderBufferedReader reader=null;//用url對象打開連接(僅僅打開了連接,并未發送請求)HttpURLConnection conn=(HttpURLConnection)url.openConnection();//設置HttpURLConnection參數//設置post方法conn.setRequestMethod("POST");//不使用緩存conn.setUseCaches(false);conn.setDefaultUseCaches(false);//post請求必須設置如下2行conn.setDoInput(true);conn.setDoOutput(true);//讀取超時時間conn.setReadTimeout(5000);conn.setConnectTimeout(5000);//設置不要302自動跳轉,防止重定向conn.setInstanceFollowRedirects(false);//設置傳入參數的格式conn.setRequestProperty("Content-Type","application/json");//沒寫可能出現411錯誤conn.setRequestProperty("Content-Length",String.valueOf(xmlData.length));//通過連接對象獲取一個輸出流DataOutputStream printout = new DataOutputStream(conn.getOutputStream());//輸出流發送請求參數printout.write(xmlData);//flush輸出流的緩沖printout.flush();printout.close();//定義BufferedReader輸入流來讀取URL的響應int code=conn.getResponseCode();System.out.println(code);if(code==200) {reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));//逐行讀取數據String line = null;while ((line = reader.readLine()) != null) {responseBuilder.append(line + "\n");}reader.close();}else{conn.getErrorStream();}String result=responseBuilder.toString();return result;}} public class WeChatTemplateMsg {/*** 消息*/private String value;/*** 消息顏色*/private String color;public WeChatTemplateMsg(String value) {this.value = value;this.color = "#173177";}public WeChatTemplateMsg(String value, String color) {this.value = value;this.color = color;}public String getValue() {return value;}public void setValue(String value) {this.value = value;}public String getColor() {return color;}public void setColor(String color) {this.color = color;} }

import com.alibaba.fastjson.JSONObject; import org.jsoup.Connection; import org.jsoup.Jsoup; import org.jsoup.nodes.Document;import org.thymeleaf.util.StringUtils;import java.io.IOException; import java.util.HashMap; import java.util.Map;public class Yi {String accessToken;public static void main(String[] args) throws Exception {Yi y=new Yi();String appid="wx122f233fcadb27ccxxxx"; //對應的idString secret="xxxx"; //對應秘鑰 就是上面那個appid下面那個Connection.Response document = Jsoup.connect("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+secret+"").timeout(4000).userAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.15").ignoreContentType(true).execute();Document parse = document.parse();String parse1 = String.valueOf(parse);String access = StringUtils.substringBefore(StringUtils.substringAfter(parse1, "access_token\":\""), "\",\"expires_in");y.accessToken=access; //獲取 // System.out.println(access);y.sendMessage();}public void sendMessage() {// 模板參數Map<String, WeChatTemplateMsg> sendMag = new HashMap<>();// openId代表一個唯一微信用戶,即微信消息的接收人String openId = "o565j6tLmAwhVMhY1UIG7ipgaNvc";// 公眾號的模板id(也有相應的接口可以查詢到)String templateId = "oXOvDQYxGmt4MBJWYJRPZvSDgKxP4Atip6bHKs-mOuM";// 微信的基礎accessTokenString url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;sendMag.put("d1", new WeChatTemplateMsg("哈哈哈嗯嗯","#28AEBD")); // sendMag.put("keyword1", new WeChatTemplateMsg("111"));//拼接base參數Map<String, Object> sendBody = new HashMap<>();sendBody.put("touser", openId); // openIdsendBody.put("url", "www.baidu.com"); // 點擊模板信息跳轉地址sendBody.put("topcolor", Close.close()); // 頂色sendBody.put("data", sendMag); // 模板參數sendBody.put("template_id", templateId); // 模板IdJSONObject json = new JSONObject(sendBody); // System.out.println(url+json);try{System.out.println(Main.getHtmlByPost(url, String.valueOf(json)));} catch (IOException e) {e.printStackTrace();}} }

總結

以上是生活随笔為你收集整理的Java代码 微信公众号推送(代码详细)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。