package com.test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL; import org.junit.Test; import net.sf.json.JSONArray;
import net.sf.json.JSONObject; public class WechatServlet {
//获取微信返回的access_token
private String getAccess_token() {
String access_token=null;
StringBuffer action =new StringBuffer();
action.append("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential")
.append("&appid=***************") //设置服务号的appid
.append("&secret=*********************************"); //设置服务号的密匙
URL url;
try {
       //模拟get请求
url = new URL(action.toString());
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("GET");
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setDoInput(true);
InputStream is =http.getInputStream();
int size =is.available();
byte[] buf=new byte[size];
is.read(buf);
String resp =new String(buf,"UTF-8");
JSONObject json = JSONObject.fromObject(resp);
Object object =json.get("access_token");
if(object !=null){
access_token =String.valueOf(object);
}
System.out.println("getAccess_token:"+access_token);
return access_token;
} catch (MalformedURLException e) {
e.printStackTrace();
return access_token; } catch (IOException e) {
e.printStackTrace();
return access_token; }
}
//获取该服务号下的用户组
public JSONArray getOpenids(){
JSONArray array =null;
String urlstr ="https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID";
urlstr =urlstr.replace("ACCESS_TOKEN", getAccess_token());
urlstr =urlstr.replace("NEXT_OPENID", "");
URL url;
try {
url = new URL(urlstr);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("GET");
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setDoInput(true);
InputStream is =http.getInputStream();
int size =is.available();
byte[] buf=new byte[size];
is.read(buf);
String resp =new String(buf,"UTF-8");
JSONObject jsonObject =JSONObject.fromObject(resp);
System.out.println("getOpenids:"+jsonObject.toString());
array =jsonObject.getJSONObject("data").getJSONArray("openid");
return array;
} catch (MalformedURLException e) {
e.printStackTrace();
return array; } catch (IOException e) {
e.printStackTrace();
return array;
}
}
//根据用户组的openId获取用户详细数据
public JSONObject getUserOpenids(String openId){
String urlstr ="https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
urlstr =urlstr.replace("ACCESS_TOKEN", getAccess_token());
urlstr =urlstr.replace("OPENID", openId);
urlstr =urlstr.replace("NEXT_OPENID", "");
URL url;
try {
url = new URL(urlstr);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("GET");
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setDoInput(true);
InputStream is =http.getInputStream();
int size =is.available();
byte[] buf=new byte[size];
is.read(buf);
String resp =new String(buf,"UTF-8");
JSONObject jsonObject =JSONObject.fromObject(resp);
System.out.println("getUserOpenids:"+jsonObject.toString());
return jsonObject;
} catch (MalformedURLException e) {
e.printStackTrace();
return null; } catch (IOException e) {
e.printStackTrace();
return null;
}
}
//主方法,只有服务号才能使用客服图文消息,实现点击图文链接,直接跳转到指定URL
@Test
public void testsendTextByOpenids(){
//String urlstr ="https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=ACCESS_TOKEN"; //群发图文消息
String urlstr ="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN"; //发送客服图文消息
urlstr =urlstr.replace("ACCESS_TOKEN", getAccess_token());
String reqjson =createGroupText(getOpenids());
try {
URL httpclient =new URL(urlstr);
HttpURLConnection conn =(HttpURLConnection) httpclient.openConnection();
conn.setConnectTimeout(5000);
conn.setReadTimeout(2000);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.connect();
OutputStream os= conn.getOutputStream();
System.out.println("ccccc:"+reqjson);
os.write(reqjson.getBytes("UTF-8"));//传入参数
os.flush();
os.close(); InputStream is =conn.getInputStream();
int size =is.available();
byte[] jsonBytes =new byte[size];
is.read(jsonBytes);
String message=new String(jsonBytes,"UTF-8");
System.out.println("testsendTextByOpenids:"+message); } catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//创建发送的数据
private String createGroupText(JSONArray array){
JSONObject gjson =new JSONObject();
//JSONObject json = getUserOpenids(array.get(3).toString()); //array参数是用户组所有的用户,该方法打印array其中一个用户的详细信息
gjson.put("touser", array.get(3));
gjson.put("msgtype", "news");
JSONObject news =new JSONObject();
JSONArray articles =new JSONArray();
JSONObject list =new JSONObject();
list.put("title","title"); //标题
list.put("description","description"); //描述
list.put("url","http://"); //点击图文链接跳转的地址
list.put("picurl","http://"); //图文链接的图片
articles.add(list);
news.put("articles", articles);
JSONObject text =new JSONObject();
gjson.put("text", text);
gjson.put("news", news); return gjson.toString();
}
}

[实例]JAVA调用微信接口发送图文消息,不用跳到详情页的更多相关文章

  1. java访问微信接口发送消息

    最近在开发activiti流程的时候有个需求:流程到达每个审批节点后,需要向该节点的审批人发送一个消息,提示有审批需要处理. 参考了一下微信的开发者文档和网络上的一些技术博客,现在记录一下.以便后续继 ...

  2. JAVA调用微信接口实现页面分享功能(分享到朋友圈显示图片,分享给朋友)

    钉钉提供的内网穿透之HTTP穿透:https://www.cnblogs.com/pxblog/p/13862376.html 网页分享到微信中如何显示标题图,如果自定义标题图,描述,显示效果如下 官 ...

  3. C# 调用微信接口上传素材和发送图文消息

    using Common;using Newtonsoft.Json.Linq;using System;using System.IO;using System.Net;using System.T ...

  4. Java之HttpClient调用WebService接口发送短信源码实战

    摘要 Java之HttpClient调用WebService接口发送短信源码实战 一:接口文档 二:WSDL 三:HttpClient方法 HttpClient方法一 HttpClient方法二 Ht ...

  5. 微信上传图文消息invalid media_id hint,thumb_media_id怎么获取

    微信上传图文消息thumb_media_id, thumb_media_id怎么获取, 微信群发图文消息invalid media_id hint, 微信群发图文消息40007, 40007,inva ...

  6. Java调用RestFul接口

    使用Java调用RestFul接口,以POST请求为例,以下提供几种方法: 一.通过HttpURLConnection调用 1 public String postRequest(String url ...

  7. Java调用webservice接口方法

                             java调用webservice接口   webservice的 发布一般都是使用WSDL(web service descriptive langu ...

  8. C# 调用微信接口的代码

    调用微信接口前需要准备的内容. 1.微信公众平台的appid 2.微信公众平台的secret 3..获取tokenid 4.获取ticket 5.生成签名的随机串 6.生成签名的时间戳 7.生成签名 ...

  9. C# 关于调用微信接口的代码

    调用微信接口前需要准备的内容. 1.微信公众平台的appid 2.微信公众平台的secret 3..获取tokenid 4.获取ticket 5.生成签名的随机串 6.生成签名的时间戳 7.生成签名 ...

随机推荐

  1. Nginx安装手册

    前提是搭建yum安装环境,见前面的教程资料 Nginx安装手册1 nginx安装环境 nginx是C语言开发,建议在linux上运行,本教程使用Centos6.5作为安装环境. gcc 安装ngin ...

  2. [学习OpenCV攻略][010][写入AVI文件]

    cvSize(文件宽度,文件高度) 通过图片或视频文件的宽高得到尺寸信息,返回值是CvSize cvCreateVideoWriter(输出文件名,编码格式,帧率,图像大小) 通过设置输出视频的格式信 ...

  3. Linux中安装opencv-3.3.1

    在ubuntu16.04中安装opencv3.3.1的过程中踩了许多坑.一开始直接安装还挺顺利但运行程序时总是提示libgtk2.0-dev和pkg-config没有安装,在安装这两个包的过程中也不顺 ...

  4. python写一个防御DDos的脚本(请安好环境否则无法实验)

    起因: 居然有ddos脚本,怎么可以没防御ddos的脚本! 开始: 1.请执行 install.py安装好DDos-defalte,会在root目录下多出这个文件夹 代码: 2.然后执行fyddos. ...

  5. hbase完全分布式安装

    hbase完全分布式安装 http://hbase.apache.org/book.html#standalone_dist                         master       ...

  6. 分布式文件系统FastDFS动态扩容

    当用户量越来越大,则集群中某个group总会到达其极限,这时就得扩展集群的容量了. FastDFS的扩容分为对group纵向扩容和横向扩容 纵向扩容 指在同一个group组中增加服务器,实现数据冗余, ...

  7. Lucene学习笔记2-Lucene的CRUD(V7.1)

    在进行CRUD的时候请注意IndexWriterConfig的设置. public class IndexCRUD { "}; private String citys[]={"j ...

  8. css3中的关键帧技术分析应用

    最近在研究网页加载进度效果的时候发现可以使用css3实现这个效果. 使用css3实现完全不需要图片,相比使用loading.gif的实现来说可能更快. 使用css3实现动态加载的效果,主要会涉及到几个 ...

  9. 让自己写的项目支持Cocoapods管理

    学会使用别人的 Pods 依赖库以后, 你一定对创建自己的依赖库很有兴趣吧,现在我们一起来制作自己的Pods依赖库. 1.创建自己的 github 仓库 上图中标识出了6处地方 Repository ...

  10. IIS命令行管理工具使用

    AppCmd.exe工具所在目录 C:\windows\sytstem32\inetsrv\目录下 一条命令批量添加应用程序 c:\Windows\System32\inetsrv>for /d ...