用的是idea,?需要修改的地方在于這些地方改了
?
?
然后吧賬號密碼填寫進(jìn)去就好了? 運(yùn)行類是? ?Yi? ?這個(gè)類
非常簡單?祝大家成功 ,不會的關(guān)注私聊??
微信公眾號測試注冊地址:微信公眾平臺 (qq.com)
?我用的模板是這個(gè)只有一個(gè)數(shù)據(jù) ,當(dāng)然這個(gè)可以自己添加
?做出來的效果當(dāng)然上面那個(gè)要添加什么只需要在這個(gè)類里面添加對應(yīng)的數(shù)據(jù)值:
?
?
?
文章來源:http://www.zghlxwxcb.cn/news/detail-520499.html
?需要導(dǎo)入的jar包文章來源地址http://www.zghlxwxcb.cn/news/detail-520499.html
<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;
//藍(lán)色
String blue;
//生成隨機(jī)對象
Random random = new Random();
//生成紅色顏色代碼
red = Integer.toHexString(random.nextInt(256)).toUpperCase();
//生成綠色顏色代碼
green = Integer.toHexString(random.nextInt(256)).toUpperCase();
//生成藍(lán)色顏色代碼
blue = Integer.toHexString(random.nextInt(256)).toUpperCase();
//判斷紅色代碼的位數(shù)
red = red.length()==1 ? "0" + red : red ;
//判斷綠色代碼的位數(shù)
green = green.length()==1 ? "0" + green : green ;
//判斷藍(lán)色代碼的位數(shù)
blue = blue.length()==1 ? "0" + blue : blue ;
//生成十六進(jìn)制顏色值
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();
//接收想要連接網(wǎng)址的地址
URL url=new URL(urlString);
//響應(yīng)
StringBuilder responseBuilder=new StringBuilder();
//讀取信息為文本信息,所以用bufferReader
BufferedReader reader=null;
//用url對象打開連接(僅僅打開了連接,并未發(fā)送請求)
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
//設(shè)置HttpURLConnection參數(shù)
//設(shè)置post方法
conn.setRequestMethod("POST");
//不使用緩存
conn.setUseCaches(false);
conn.setDefaultUseCaches(false);
//post請求必須設(shè)置如下2行
conn.setDoInput(true);
conn.setDoOutput(true);
//讀取超時(shí)時(shí)間
conn.setReadTimeout(5000);
conn.setConnectTimeout(5000);
//設(shè)置不要302自動跳轉(zhuǎn),防止重定向
conn.setInstanceFollowRedirects(false);
//設(shè)置傳入?yún)?shù)的格式
conn.setRequestProperty("Content-Type","application/json");
//沒寫可能出現(xiàn)411錯(cuò)誤
conn.setRequestProperty("Content-Length",String.valueOf(xmlData.length));
//通過連接對象獲取一個(gè)輸出流
DataOutputStream printout = new DataOutputStream(conn.getOutputStream());
//輸出流發(fā)送請求參數(shù)
printout.write(xmlData);
//flush輸出流的緩沖
printout.flush();
printout.close();
//定義BufferedReader輸入流來讀取URL的響應(yīng)
int code=conn.getResponseCode();
System.out.println(code);
if(code==200) {
reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
//逐行讀取數(shù)據(jù)
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"; //對應(yīng)的id
String secret="xxxx"; //對應(yīng)秘鑰 就是上面那個(gè)appid下面那個(gè)
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() {
// 模板參數(shù)
Map<String, WeChatTemplateMsg> sendMag = new HashMap<>();
// openId代表一個(gè)唯一微信用戶,即微信消息的接收人
String openId = "o565j6tLmAwhVMhY1UIG7ipgaNvc";
// 公眾號的模板id(也有相應(yīng)的接口可以查詢到)
String templateId = "oXOvDQYxGmt4MBJWYJRPZvSDgKxP4Atip6bHKs-mOuM";
// 微信的基礎(chǔ)accessToken
String 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參數(shù)
Map<String, Object> sendBody = new HashMap<>();
sendBody.put("touser", openId); // openId
sendBody.put("url", "www.baidu.com"); // 點(diǎn)擊模板信息跳轉(zhuǎn)地址
sendBody.put("topcolor", Close.close()); // 頂色
sendBody.put("data", sendMag); // 模板參數(shù)
sendBody.put("template_id", templateId); // 模板Id
JSONObject json = new JSONObject(sendBody);
// System.out.println(url+json);
try{
System.out.println(Main.getHtmlByPost(url, String.valueOf(json)));
} catch (IOException e) {
e.printStackTrace();
}
}
}
到了這里,關(guān)于Java代碼 微信公眾號推送(代碼詳細(xì))的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!