软录 · 2023年4月18日

java微信协议发送数据|java如何调用微信接口发送文件到微信群

⑴ 如何用java给指定的微信用户推送消息,每天早上十点推送一条消息。麻烦说的仔细一点,最好能给出实例谢谢

首先有自己的web服务器,申请域名。

申请微信公众平台的开发者资格,申请一个公众号。

在自己的服务器端开发定时任务程序,程序主要流程是两部,一:按公众平台的认证接口获取公众号认证(获取令牌),二:调用公众平台消息推送接口(RESTful风格的接口)

总之需要研究微信公众平台的接口说明。

http://mp.weixin.qq.com/wiki/index.php?title=%E6%B6%88%E6%81%AF%E6%8E%A5%E5%8F%A3%E6%8C%87%E5%8D%97

⑵ 如何用java开发微信

说明:本次的教程主要是对微信公众平台开发者模式的讲解,网络上很多类似文章,但很多都让初学微信开发的人一头雾水,所以总结自己的微信开发经验,将微信开发的整个过程系统的列出,并对主要代码进行讲解分析,让初学者尽快上手。在阅读本文之前,应对微信公众平台的官方开发文档有所了解,知道接收和发送的都是xml格式的数据。另外,在做内容回复时用到了图灵机器人的api接口,这是一个自然语言解析的开放平台,可以帮我们解决整个微信开发过程中最困难的问题,此处不多讲,下面会有其详细的调用方式。

1.1 在登录微信官方平台之后,开启开发者模式,此时需要我们填写url和token,所谓url就是我们自己服务器的接口,用WechatServlet.java来实现,相关解释已经在注释中说明,代码如下:

[java]view plain

packagedemo.servlet;

importjava.io.BufferedReader;

importjava.io.IOException;

importjava.io.InputStream;

importjava.io.InputStreamReader;

importjava.io.OutputStream;

importjavax.servlet.ServletException;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importdemo.process.WechatProcess;

/**

*微信服务端收发消息接口

*

*@authorpamchen-1

*

*/

{

/**

*ThedoGetmethodoftheservlet.<br>

*

*.

*

*@paramrequest

*

*@paramresponse

*

*@throwsServletException

*ifanerroroccurred

*@throwsIOException

*ifanerroroccurred

*/

publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

request.setCharacterEncoding("UTF-8");

response.setCharacterEncoding("UTF-8");

/**读取接收到的xml消息*/

StringBuffersb=newStringBuffer();

InputStreamis=request.getInputStream();

InputStreamReaderisr=newInputStreamReader(is,"UTF-8");

BufferedReaderbr=newBufferedReader(isr);

Strings="";

while((s=br.readLine())!=null){

sb.append(s);

}

Stringxml=sb.toString();//次即为接收到微信端发送过来的xml数据

Stringresult="";

/**判断是否是微信接入激活验证,只有首次接入验证时才会收到echostr参数,此时需要把它直接返回*/

Stringechostr=request.getParameter("echostr");

if(echostr!=null&&echostr.length()>1){

result=echostr;

}else{

//正常的微信处理流程

result=newWechatProcess().processWechatMag(xml);

}

try{

OutputStreamos=response.getOutputStream();

os.write(result.getBytes("UTF-8"));

os.flush();

os.close();

}catch(Exceptione){

e.printStackTrace();

}

}

/**

*ThedoPostmethodoftheservlet.<br>

*

*

*post.

*

*@paramrequest

*

*@paramresponse

*

*@throwsServletException

*ifanerroroccurred

*@throwsIOException

*ifanerroroccurred

*/

publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

doGet(request,response);

}

}

1.2 相应的web.xml配置信息如下,在生成WechatServlet.java的同时,可自动生成web.xml中的配置。前面所提到的url处可以填写例如:http;//服务器地址/项目名/wechat.do

[html]view plain

<?xmlversion="1.0"encoding="UTF-8"?>

<web-appversion="2.5"

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>

<description></description>

<display-name></display-name>

<servlet-name>WechatServlet</servlet-name>

<servlet-class>demo.servlet.WechatServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>WechatServlet</servlet-name>

<url-pattern>/wechat.do</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

</web-app>

1.3 通过以上代码,我们已经实现了微信公众平台开发的框架,即开通开发者模式并成功接入、接收消息和发送消息这三个步骤。

下面就讲解其核心部分——解析接收到的xml数据,并以文本类消息为例,通过图灵机器人api接口实现智能回复。2.1 首先看一下整体流程处理代码,包括:xml数据处理、调用图灵api、封装返回的xml数据。

[java]view plain

packagedemo.process;

importjava.util.Date;

importdemo.entity.ReceiveXmlEntity;

/**

*微信xml消息处理流程逻辑类

*@authorpamchen-1

*

*/

publicclassWechatProcess{

/**

*解析处理xml、获取智能回复结果(通过图灵机器人api接口)

*@paramxml接收到的微信数据

*@return最终的解析结果(xml格式数据)

*/

publicStringprocessWechatMag(Stringxml){

/**解析xml数据*/

ReceiveXmlEntityxmlEntity=newReceiveXmlProcess().getMsgEntity(xml);

/**以文本消息为例,调用图灵机器人api接口,获取回复内容*/

Stringresult="";

if("text".endsWith(xmlEntity.getMsgType())){

result=newTulingApiProcess().getTulingResult(xmlEntity.getContent());

}

/**此时,如果用户输入的是“你好”,在经过上面的过程之后,result为“你也好”类似的内容

*因为最终回复给微信的也是xml格式的数据,所有需要将其封装为文本类型返回消息

**/

result=newFormatXmlProcess().formatXmlAnswer(xmlEntity.getFromUserName(),xmlEntity.getToUserName(),result);

returnresult;

}

}

2.2 解析接收到的xml数据,此处有两个类,ReceiveXmlEntity.java和ReceiveXmlProcess.java,通过反射的机制动态调用实体类中的set方法,可以避免很多重复的判断,提高代码效率,代码如下:

[java]view plain

packagedemo.entity;

/**

*接收到的微信xml实体类

*@authorpamchen-1

*

*/

publicclassReceiveXmlEntity{

privateStringToUserName="";

privateStringFromUserName="";

privateStringCreateTime="";

privateStringMsgType="";

privateStringMsgId="";

privateStringEvent="";

privateStringEventKey="";

privateStringTicket="";

privateStringLatitude="";

privateStringLongitude="";

privateStringPrecision="";

privateStringPicUrl="";

privateStringMediaId="";

privateStringTitle="";

privateStringDescription="";

privateStringUrl="";

privateStringLocation_X="";

privateStringLocation_Y="";

privateStringScale="";

privateStringLabel="";

privateStringContent="";

privateStringFormat="";

privateStringRecognition="";

publicStringgetRecognition(){

returnRecognition;

}

publicvoidsetRecognition(Stringrecognition){

Recognition=recognition;

}

publicStringgetFormat(){

returnFormat;

}

publicvoidsetFormat(Stringformat){

Format=format;

}

publicStringgetContent(){

returnContent;

}

publicvoidsetContent(Stringcontent){

Content=content;

}

publicStringgetLocation_X(){

returnLocation_X;

}

publicvoidsetLocation_X(StringlocationX){

Location_X=locationX;

}

publicStringgetLocation_Y(){

returnLocation_Y;

}

publicvoidsetLocation_Y(StringlocationY){

Location_Y=locationY;

}

publicStringgetScale(){

returnScale;

}

publicvoidsetScale(Stringscale){

Scale=scale;

}

publicStringgetLabel(){

returnLabel;

}

publicvoidsetLabel(Stringlabel){

Label=label;

}

publicStringgetTitle(){

returnTitle;

}

publicvoidsetTitle(Stringtitle){

Title=title;

}

publicStringgetDescription(){

returnDescription;

}

publicvoidsetDescription(Stringdescription){

Description=description;

}

publicStringgetUrl(){

returnUrl;

}

publicvoidsetUrl(Stringurl){

Url=url;

}

publicStringgetPicUrl(){

returnPicUrl;

}

publicvoidsetPicUrl(StringpicUrl){

PicUrl=picUrl;

}

publicStringgetMediaId(){

returnMediaId;

}

publicvoidsetMediaId(StringmediaId){

MediaId=mediaId;

}

publicStringgetEventKey(){

returnEventKey;

}

publicvoidsetEventKey(StringeventKey){

EventKey=eventKey;

}

publicStringgetTicket(){

returnTicket;

}

publicvoidsetTicket(Stringticket){

Ticket=ticket;

}

publicStringgetLatitude(){

returnLatitude;

}

publicvoidsetLatitude(Stringlatitude){

Latitude=latitude;

}

publicStringgetLongitude(){

returnLongitude;

}

publicvoidsetLongitude(Stringlongitude){

Longitude=longitude;

}

publicStringgetPrecision(){

returnPrecision;

}

publicvoidsetPrecision(Stringprecision){

Precision=precision;

}

publicStringgetEvent(){

returnEvent;

}

publicvoidsetEvent(Stringevent){

Event=event;

}

publicStringgetMsgId(){

returnMsgId;

}

publicvoidsetMsgId(StringmsgId){

MsgId=msgId;

}

publicStringgetToUserName(){

returnToUserName;

}

publicvoidsetToUserName(StringtoUserName){

⑶ 微信公众平台的服务号用java怎么向用户发送消息

网络搜索 柳峰http://blog.csdn.net/lyq8479/

⑷ java程序 向微信用户发送消息

微信开放平台里有高级功能,可以群发信息给用户

⑸ 如何实现java程序与微信公众平台之间实现消息推送

微信开发者 文档上有相应的API,仔细查看api文档,很简单的。

⑹ java如何调用微信接口发送文件到微信群

目前微信没有开放发送文件到微信群的api,毕竟开放了很有可能会被微商利用,只能上传至公众号的图文素材,再进行推送。但不管是订阅号还是服务号,推送都有限制。

⑺ java实现微信发送消息

net的我有 java的还没看呢 给你说说原理 通过开发者id 或者关注者列表 然后通过用户openid(用户唯一标专示)向用属户发送客服消息 他这个通道是走的客服消息 ,前提是必须关注者主动向公众号发过消息 时限为24h

⑻ java服务端怎么主动给某用户发送消息

如果你是 安卓客户端 用webview 打开一个html页面, 那你就去搜 WEB推送。如果是 安卓原生应用的话,你应该挂一个长连接 都服务端,然后服务端推送信息即可。有很多提供推送功能,即时通讯,的都可以, 个推,xmpp协议是即时通讯的协议,实现这样功能的服务器,也可以满足你这个要求。有很多这样的协议,我也记不住那么多。如果你要自己写的话,可以用下mina,跟netty吧。以前我跟我同事用过mina做安卓的推送。

⑼ 如何用java程序给微信用户发送消息,麻烦请给出实例,谢谢!急求

两种方式:1 用微信公众平台提供的接口2 直接抓取微信公众平台网站上的数据包(几乎所有请求都是ajax形式,并返回Json数组的),用httpclient来模拟。#1的好处是:微信提供的接口,名正言顺。 缺点是:1 接口太少;2 不能主动向某个用户说话(因为推送似乎一天就一条),必须用户发起给微信平台,微信平台再通过http调用你的系统,你不能反过来搞;3 好像还有时间限制,超过几秒就断了。

⑽ java web怎样实现分享到微信

代码很简单,我这里就不多废话了,直接奉上源码:

复制代码代码如下:

<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%><%Stringpath=request.getContextPath();StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"><html><head><basehref="<%=basePath%>"><title>喝喝</title><metahttp-equiv="pragma"content="no-cache"><metahttp-equiv="cache-control"content="no-cache"><metahttp-equiv="expires"content="0"><metahttp-equiv="keywords"content="keyword1,keyword2,keyword3"><metahttp-equiv="description"content="Thisismypage"><!–<linkrel="stylesheet"type="text/css"href="styles.css">–><script>varimgUrl='varlineLink='vardescContent="万达狂欢节,夺宝幸运星大抽奖活动开始啦!";varshareTitle='万达狂欢节';varappid='wxc9937e3a66af6dc8';functionshareFriend(){WeixinJSBridge.invoke('sendAppMessage',{"appid":appid,"img_url":imgUrl,"img_width":"640","img_height":"640","link":lineLink,"desc":descContent,"title":shareTitle},function(res){_report('send_msg',res.err_msg);})}functionshareTimeline(){WeixinJSBridge.invoke('shareTimeline',{"img_url":imgUrl,"img_width":"640","img_height":"640","link":lineLink,"desc":descContent,"title":shareTitle},function(res){_report('timeline',res.err_msg);});}functionshareWeibo(){WeixinJSBridge.invoke('shareWeibo',{"content":descContent,"url":lineLink,},function(res){_report('weibo',res.err_msg);});}//当微信内置浏览器完成内部初始化后会触发WeixinJSBridgeReady事件。document.addEventListener('WeixinJSBridgeReady',functiononBridgeReady(){//发送给好友WeixinJSBridge.on('menu:share:appmessage',function(argv){shareFriend();});//分享到朋友圈WeixinJSBridge.on('menu:share:timeline',function(argv){shareTimeline();});//分享到微博WeixinJSBridge.on('menu:share:weibo',function(argv){shareWeibo();});},false);</script></head><body><!–<script>varimgUrl="varlineLink="vardescContent='测试别当真';varshareTitle='分享';varappid='wx1259b351c201841d';functionshareFriend(){WeixinJSBridge.invoke('sendAppMessage',{"appid":appid,"img_url":imgUrl,"img_width":"200","img_height":"200","link":lineLink,"desc":descContent,"title":shareTitle},function(res){//_report('send_msg',res.err_msg);});}functionshareTimeline(){WeixinJSBridge.invoke('shareTimeline',{"img_url":imgUrl,"img_width":"200","img_height":"200","link":lineLink,"desc":descContent,"title":shareTitle},function(res){//_report('timeline',res.err_msg);});}functionshareWeibo(){WeixinJSBridge.invoke('shareWeibo',{"content":descContent,"url":lineLink,},function(res){//_report('weibo',res.err_msg);});}//当微信内置浏览器完成内部初始化后会触发WeixinJSBridgeReady事件。document.addEventListener('WeixinJSBridgeReady',functiononBridgeReady(){//发送给好友WeixinJSBridge.on('menu:share:appmessage',function(argv){shareFriend();});//分享到朋友圈WeixinJSBridge.on('menu:share:timeline',function(argv){shareTimeline();});//分享到微博WeixinJSBridge.on('menu:share:weibo',function(argv){shareWeibo();});},false);</script>–><h1>呵呵呵呵</h1></body></html>