欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

微信企业号验证/发送/接收消息

程序员文章站 2024-03-12 20:45:20
1. 内网映射 由于微信企业号回调模式的url尽支持域名方式访问,估需要注册花生壳,做一个内网穿透(需要花16块钱,购买一个免费版,购买之后,第二天才能添加上域名) 2...

1. 内网映射

由于微信企业号回调模式的url尽支持域名方式访问,估需要注册花生壳,做一个内网穿透(需要花16块钱,购买一个免费版,购买之后,第二天才能添加上域名)

2. 微信企业号

注册微信企业号: (选择团队,团队不需要认证)

通讯录:新建组织 - > 关注成员

企业号 -> 应用中心 -> 新建应用 -> 消息型应用 -> 模式选择(回调模式) -> 开启微信消息转发,
回调模式说明:http://qydev.weixin.qq.com/wiki/index.php?title=%e5%9b%9e%e8%b0%83%e6%a8%a1%e5%bc%8f
回调模式加密解密代码:http://qydev.weixin.qq.com/wiki/index.php?title=%e5%8a%a0%e8%a7%a3%e5%af%86%e5%ba%93%e4%b8%8b%e8%bd%bd%e4%b8%8e%e8%bf%94%e5%9b%9e%e7%a0%81

如图1:

微信企业号验证/发送/接收消息

自定义菜单: 开发应用的请求路径如图2:

微信企业号验证/发送/接收消息

设置 -> 功能设置 -> 权限管理 -> 新建管理组 -> 应用权限( secret )

3. 利用jersey开发web service服务

3.1 在类中定义token, 随机密码43位,公司corpid, secret

3.2 验证方法

/* 
* ------------使用示例一:验证回调url---------------企业开启回调模式时,企业号会向验证url发送一个get请求 
* 假设点击验证时,企业收到类似请求: get 
* /cgi-bin/wxpush?msg_signature=5c45ff5e21c57e6ad56bac8758b79b1d9ac89fd3 
* ×tamp 
* =1409659589&nonce=263014780&echostr=p9nazczydtytweshep1vc5x9xho% 
* 2fqyx3zpb4yka9skld1dsh3iyt3tp3zndtp%2b4rpcs8tgae7oabo%2bfzxvnaqq%3d%3d 
* http/1.1 host: qy.weixin.qq.com 
* 
* 接收到该请求时,企业应 
* 1.解析出get请求的参数,包括消息体签名(msg_signature),时间戳(timestamp),随机数字串(nonce 
* )以及公众平台推送过来的随机加密字符串(echostr), 这一步注意作url解码。 2.验证消息体签名的正确性 3. 
* 解密出echostr原文,将原文当作get请求的response,返回给公众平台 第2,3步可以用公众平台提供的库函数verifyurl来实现。 
*/ 
/** 
* 回调url,微信调用此方法进行验证 
* 
* @return 
*/ 
@get 
@path("station") 
public string verify() { 
string msgsignature = request.getparameter("msg_signature"); 
string timestamp = request.getparameter("timestamp"); 
string nonce = request.getparameter("nonce"); 
system.out.println(timestamp + " " + nonce); 
string echostr = request.getparameter("echostr"); 
string sechostr = null; 
try { 
sechostr = wxcpt.verifyurl(msgsignature, timestamp, nonce, echostr); 
} catch (exception e) { 
e.printstacktrace();// 验证url失败,错误原因请查看异常 
} 
return sechostr; 
}

3.3 接收用户信息,并解密

/* 
* ------------对用户回复的消息解密--------------- 
* 用户回复消息或者点击事件响应时,企业会收到回调消息,此消息是经过公众平台加密之后的密文以post形式发送给企业,密文格式请参考官方文档 
* 假设企业收到公众平台的回调消息如下: post /cgi-bin/wxpush? 
* msg_signature=477715d11cdb4164915debcba66cb864d751f3e6 
* ×tamp=1409659813&nonce=1372623149 http/1.1 host: qy.weixin.qq.com 
* content-length: 613 <xml> 
* <tousername><![cdata[wx5823bf96d3bd56c7]]></tousername 
* ><encrypt><![cdata[rypevhkd8qqkfhvq6qleeb4j58tipdvo 
* +rtk1i9qca6am/wvqnlsv5zepeusuix5l5x/0lwfrf0qadhhhgd3qczcdcupj911l3vg3w/ 
* syyvujts3tuuksuxxaccas0qhxchrryt66wispglyl42am6a8dtt 
* +6k4asknmpj48kzjs8qljvd4xgpue06dodnlxauhzm6 
* +kdz+hmzfjyur+ltwgc2hgf5gsijff0ekunxziqatp7pf5mzxz3izoun1s4zg4lumnvw2r 
* +kqckiw 
* +3iqh03v+bca9nmelnqbsf6tiwsrxjb3lavgucallcrw8v2t9el4ehzjwrquax5wlvmns0 
* +rupa3k22ncx4xxzs9o0mbh27bo6bpnelzps 
* +/uh9ksnly6bhcmju9p8g7m3fvkn28h3kdya5pl 
* /t8z1ptdave0lxdq2yoyyh2uypighbzzis2pdbs8r07+qn+e7q==]]></encrypt> 
* <agentid><![cdata[218]]></agentid> </xml> 
* 
* 企业收到post请求之后应该 
* 1.解析出url上的参数,包括消息体签名(msg_signature),时间戳(timestamp)以及随机数字串(nonce) 
* 2.验证消息体签名的正确性。 
* 3.将post请求的数据进行xml解析,并将<encrypt>标签的内容进行解密,解密出来的明文即是用户回复消息的明文,明文格式请参考官方文档 
* 第2,3步可以用公众平台提供的库函数decryptmsg来实现。 
*/ 
@post 
@path("station") 
public string receivemsg(string reqdata) { 
string msgsignature = request.getparameter("msg_signature"); 
string timestamp = request.getparameter("timestamp"); 
string nonce = request.getparameter("nonce"); 
// post请求的密文数据 
// string sreqdata = 
// "<xml><tousername><![cdata[wx5823bf96d3bd56c7]]></tousername><encrypt><![cdata[rypevhkd8qqkfhvq6qleeb4j58tipdvo+rtk1i9qca6am/wvqnlsv5zepeusuix5l5x/0lwfrf0qadhhhgd3qczcdcupj911l3vg3w/syyvujts3tuuksuxxaccas0qhxchrryt66wispglyl42am6a8dtt+6k4asknmpj48kzjs8qljvd4xgpue06dodnlxauhzm6+kdz+hmzfjyur+ltwgc2hgf5gsijff0ekunxziqatp7pf5mzxz3izoun1s4zg4lumnvw2r+kqckiw+3iqh03v+bca9nmelnqbsf6tiwsrxjb3lavgucallcrw8v2t9el4ehzjwrquax5wlvmns0+rupa3k22ncx4xxzs9o0mbh27bo6bpnelzps+/uh9ksnly6bhcmju9p8g7m3fvkn28h3kdya5pl/t8z1ptdave0lxdq2yoyyh2uypighbzzis2pdbs8r07+qn+e7q==]]></encrypt><agentid><![cdata[218]]></agentid></xml>"; 
try { 
string smsg = wxcpt.decryptmsg(msgsignature, timestamp, nonce, 
reqdata); 
// 解析出明文xml标签的内容进行处理 
documentbuilderfactory dbf = documentbuilderfactory.newinstance(); 
documentbuilder db = dbf.newdocumentbuilder(); 
stringreader sr = new stringreader(smsg); 
inputsource is = new inputsource(sr); 
document document = db.parse(is); 
element root = document.getdocumentelement(); 
nodelist nodelist1 = root.getelementsbytagname("content"); 
if (nodelist1.item(0) == null) 
return "ok"; 
string content = nodelist1.item(0).gettextcontent(); 
system.out.println("content:" + content); 
} catch (exception e) { 
e.printstacktrace();// 解密失败,失败原因请查看异常 
} 
return "ok"; 
}

3.4 发送信息到微信

设置 -> 功能设置 -> 权限管理 -> 新建管理组; 获取secret

微信企业号验证/发送/接收消息

/** 
* 此方法可以发送任意类型消息 
* 
* @param msgtype 
* text|image|voice|video|file|news 
* @param touser 
* 成员id列表(消息接收者,多个接收者用‘|'分隔,最多支持1000个)。特殊情况:指定为@all, 
* 则向关注该企业应用的全部成员发送 
* @param toparty 
* 部门id列表,多个接收者用‘|'分隔,最多支持100个。当touser为@all时忽略本参数 
* @param totag 
* 标签id列表,多个接收者用‘|'分隔。当touser为@all时忽略本参数 
* @param content 
* msgtype=text时 ,文本消息内容 
* @param mediaid 
* msgtype=image|voice|video时 ,对应消息信息id(--------) 
* @param title 
* msgtype=news|video时,消息标题 
* @param description 
* msgtype=news|video时,消息描述 
* @param url 
* msgtype=news时,消息链接 
* @param picurl 
* msgtype=news时,图片路径 
* @param safe 
* 表示是否是保密消息,0表示否,1表示是,默认0 
*/ 
public void sendwechatmsg(string msgtype, string touser, string toparty, 
string totag, string content, string mediaid, string title, 
string description, string url, string picurl, string safe) { 
url url; 
string access_token = getaccesstoken(); 
// 拼接请求串 
string action = create_session_url + access_token; 
// 封装发送消息请求json 
stringbuffer sb = new stringbuffer(); 
sb.append("{"); 
sb.append("\"touser\":" + "\"" + touser + "\","); 
sb.append("\"toparty\":" + "\"" + toparty + "\","); 
sb.append("\"totag\":" + "\"" + totag + "\","); 
if (msgtype.equals("text")) { 
sb.append("\"msgtype\":" + "\"" + msgtype + "\","); 
sb.append("\"text\":" + "{"); 
sb.append("\"content\":" + "\"" + content + "\""); 
sb.append("}"); 
} else if (msgtype.equals("image")) { 
sb.append("\"msgtype\":" + "\"" + msgtype + "\","); 
sb.append("\"image\":" + "{"); 
sb.append("\"media_id\":" + "\"" + mediaid + "\""); 
sb.append("}"); 
} else if (msgtype.equals("voice")) { 
sb.append("\"msgtype\":" + "\"" + msgtype + "\","); 
sb.append("\"voice\":" + "{"); 
sb.append("\"media_id\":" + "\"" + mediaid + "\""); 
sb.append("}"); 
} else if (msgtype.equals("video")) { 
sb.append("\"msgtype\":" + "\"" + msgtype + "\","); 
sb.append("\"video\":" + "{"); 
sb.append("\"media_id\":" + "\"" + mediaid + "\","); 
sb.append("\"title\":" + "\"" + title + "\","); 
sb.append("\"description\":" + "\"" + description + "\""); 
sb.append("}"); 
} else if (msgtype.equals("file")) { 
sb.append("\"msgtype\":" + "\"" + msgtype + "\","); 
sb.append("\"file\":" + "{"); 
sb.append("\"media_id\":" + "\"" + mediaid + "\""); 
sb.append("}"); 
} else if (msgtype.equals("news")) { 
sb.append("\"msgtype\":" + "\"" + msgtype + "\","); 
sb.append("\"news\":" + "{"); 
sb.append("\"articles\":" + "["); 
sb.append("{"); 
sb.append("\"title\":" + "\"" + title + "\","); 
sb.append("\"description\":" + "\"" + description + "\","); 
sb.append("\"url\":" + "\"" + url + "\","); 
sb.append("\"picurl\":" + "\"" + picurl + "\""); 
sb.append("}"); 
sb.append("]"); 
sb.append("}"); 
} 
sb.append(",\"safe\":" + "\"" + safe + "\","); 
sb.append("\"agentid\":" + "\"" + 1 + "\","); 
sb.append("\"debug\":" + "\"" + "1" + "\""); 
sb.append("}"); 
string json = sb.tostring(); 
try { 
url = new url(action); 
httpsurlconnection http = (httpsurlconnection) url.openconnection(); 
http.setrequestmethod("post"); 
http.setrequestproperty("content-type", 
"application/json;charset=utf-8"); 
http.setdooutput(true); 
http.setdoinput(true); 
system.setproperty("sun.net.client.defaultconnecttimeout", "30000");// 
// 连接超时30秒 
system.setproperty("sun.net.client.defaultreadtimeout", "30000"); // 
// 读取超时30秒 
http.connect(); 
outputstream os = http.getoutputstream(); 
os.write(json.getbytes("utf-8"));// 传入参数 
inputstream is = http.getinputstream(); 
int size = is.available(); 
byte[] jsonbytes = new byte[size]; 
is.read(jsonbytes); 
string result = new string(jsonbytes, "utf-8"); 
system.out.println("请求返回结果:" + result); 
os.flush(); 
os.close(); 
} catch (exception e) { 
e.printstacktrace(); 
} 
} 
// 获取接口访问权限码 
public string getaccesstoken() { 
httpclient client = new httpclient(); 
postmethod post = new postmethod(access_token_url); 
post.releaseconnection(); 
post.setrequestheader("content-type", 
"application/x-www-form-urlencoded;charset=utf-8"); 
namevaluepair[] param = { new namevaluepair("corpid", corpid), 
new namevaluepair("corpsecret", secret) }; 
// 设置策略,防止报cookie错误 
defaulthttpparams.getdefaultparams().setparameter( 
"http.protocol.cookie-policy", 
cookiepolicy.browser_compatibility); 
// 给post设置参数 
post.setrequestbody(param); 
string result = ""; 
try { 
client.executemethod(post); 
result = new string(post.getresponsebodyasstring().getbytes("gbk")); 
} catch (ioexception e) { 
e.printstacktrace(); 
} 

// 将数据转换成json 
jsonobject jasonobject; 

jasonobject = jsonobject.fromobject(result); 
result = (string) jasonobject.get("access_token"); 

post.releaseconnection(); 
system.out.println(result); 
return result; 

} 
public static void main(string[] args) { 
stationresource wechat = new stationresource(); 
// wechat.sendwechatmsgtext("@all", "2", "", "信息中心通知", "0"); 
wechat.sendwechatmsg("news", "@all", "", "", "测试senmsg", "", "测试的", 
"真的是测试的", "http://www.baidu.com", 
"http://file27.mafengwo.net/m00/b2/12/wkgb6lo0ahwamhl8aav1ybfjdjw20.jpeg", "0"); 
}

4. 开发完成。 需要将该类在webx.xml中添加到rest中管理

<!-- restful支持 --> 
<!-- webserivce服务,如增加了服务,需要在param-value中增加服务的包路径 --> 
<servlet> 
<servlet-name>jax-rs rest servlet</servlet-name> 
<servlet-class>com.sun.jersey.spi.container.servlet.servletcontainer</servlet-class> 
<init-param> 
<param-name>com.sun.jersey.config.property.packages</param-name> 
<param-value>com.base.pf.restful</param-value> 
</init-param> 
<load-on-startup>2</load-on-startup> 
</servlet> 
<servlet-mapping> 
<servlet-name>jax-rs rest servlet</servlet-name> 
<url-pattern>/rest/*</url-pattern> 
</servlet-mapping>

5. 开发完成的全量代码

package com.base.pf.restful; 
import java.io.ioexception; 
import java.io.inputstream; 
import java.io.outputstream; 
import java.io.stringreader; 
import java.net.url; 
import javax.net.ssl.httpsurlconnection; 
import javax.servlet.http.httpservletrequest; 
import javax.servlet.http.httpservletresponse; 
import javax.ws.rs.get; 
import javax.ws.rs.post; 
import javax.ws.rs.path; 
import javax.ws.rs.core.context; 
import javax.xml.parsers.documentbuilder; 
import javax.xml.parsers.documentbuilderfactory; 
import net.sf.json.jsonobject; 
import org.apache.commons.httpclient.httpclient; 
import org.apache.commons.httpclient.namevaluepair; 
import org.apache.commons.httpclient.cookie.cookiepolicy; 
import org.apache.commons.httpclient.methods.postmethod; 
import org.apache.commons.httpclient.params.defaulthttpparams; 
import org.w3c.dom.document; 
import org.w3c.dom.element; 
import org.w3c.dom.nodelist; 
import org.xml.sax.inputsource; 
import com.qq.weixin.mp.aes.aesexception; 
import com.qq.weixin.mp.aes.wxbizmsgcrypt; 
/** 
* 微信企业号开发 
* 
* @author zhen.l 
* 
*/ 
@path("wx") 
public class stationresource { 
// http://hichinamobile.xicp.net/security/rest/wx 
// https://qy.weixin.qq.com 
private string token = "spm"; // 企业号 -> 应用中心 -> 新建应用 -> 消息型应用 
private string agentid = "1"; // 企业号 -> 应用中心 -> 点开应用中 -> 应用id 
private string encodingaeskey = "nt6zwtvflynxohfoggozwdjpagefsv8ln5cneyw7mwl"; // 企业号 -> 应用中心 -> 新建应用 -> 消息型应用 
private string corpid = "wxe49318eb604cf00b"; // 企业号 -> 设置 -> 企业号信息 -> 账号信息 
private string secret = "m-yfkmgl_kxbvetginzh3rqwbz4xb6mfeqxxlk77mkpxzenfdyq-ugerxduf8rel"; // 企业号 -> 设置 -> 功能设置 -> 权限管理中 
@context 
httpservletrequest request; 
@context 
httpservletresponse response; 
wxbizmsgcrypt wxcpt = null; 
public stationresource() { 
try { 
wxcpt = new wxbizmsgcrypt(token, encodingaeskey, corpid); 
} catch (aesexception e) { 
e.printstacktrace(); 
} 
} 
// 获取访问权限码url 
private final static string access_token_url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"; 
// 创建会话请求url 
private final static string create_session_url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="; 
// 获取接口访问权限码 
public string getaccesstoken() { 
httpclient client = new httpclient(); 
postmethod post = new postmethod(access_token_url); 
post.releaseconnection(); 
post.setrequestheader("content-type", 
"application/x-www-form-urlencoded;charset=utf-8"); 
namevaluepair[] param = { new namevaluepair("corpid", corpid), 
new namevaluepair("corpsecret", secret) }; 
// 设置策略,防止报cookie错误 
defaulthttpparams.getdefaultparams().setparameter( 
"http.protocol.cookie-policy", 
cookiepolicy.browser_compatibility); 
// 给post设置参数 
post.setrequestbody(param); 
string result = ""; 
try { 
client.executemethod(post); 
result = new string(post.getresponsebodyasstring().getbytes("gbk")); 
} catch (ioexception e) { 
e.printstacktrace(); 
} 
// 将数据转换成json 
jsonobject jasonobject; 
jasonobject = jsonobject.fromobject(result); 
result = (string) jasonobject.get("access_token"); 
post.releaseconnection(); 
system.out.println(result); 
return result; 
} 
/* 
* ------------使用示例一:验证回调url---------------企业开启回调模式时,企业号会向验证url发送一个get请求 
* 假设点击验证时,企业收到类似请求: get 
* /cgi-bin/wxpush?msg_signature=5c45ff5e21c57e6ad56bac8758b79b1d9ac89fd3 
* ×tamp 
* =1409659589&nonce=263014780&echostr=p9nazczydtytweshep1vc5x9xho% 
* 2fqyx3zpb4yka9skld1dsh3iyt3tp3zndtp%2b4rpcs8tgae7oabo%2bfzxvnaqq%3d%3d 
* http/1.1 host: qy.weixin.qq.com 
* 
* 接收到该请求时,企业应 
* 1.解析出get请求的参数,包括消息体签名(msg_signature),时间戳(timestamp),随机数字串(nonce 
* )以及公众平台推送过来的随机加密字符串(echostr), 这一步注意作url解码。 2.验证消息体签名的正确性 3. 
* 解密出echostr原文,将原文当作get请求的response,返回给公众平台 第2,3步可以用公众平台提供的库函数verifyurl来实现。 
*/ 
/** 
* 回调url,微信调用此方法进行验证 
* 
* @return 
*/ 
@get 
@path("station") 
public string verify() { 
string msgsignature = request.getparameter("msg_signature"); 
string timestamp = request.getparameter("timestamp"); 
string nonce = request.getparameter("nonce"); 
system.out.println(timestamp + " " + nonce); 
string echostr = request.getparameter("echostr"); 
string sechostr = null; 
try { 
sechostr = wxcpt.verifyurl(msgsignature, timestamp, nonce, echostr); 
} catch (exception e) { 
e.printstacktrace();// 验证url失败,错误原因请查看异常 
} 
return sechostr; 
} 
/* 
* ------------对用户回复的消息解密--------------- 
* 用户回复消息或者点击事件响应时,企业会收到回调消息,此消息是经过公众平台加密之后的密文以post形式发送给企业,密文格式请参考官方文档 
* 假设企业收到公众平台的回调消息如下: post /cgi-bin/wxpush? 
* msg_signature=477715d11cdb4164915debcba66cb864d751f3e6 
* ×tamp=1409659813&nonce=1372623149 http/1.1 host: qy.weixin.qq.com 
* content-length: 613 <xml> 
* <tousername><![cdata[wx5823bf96d3bd56c7]]></tousername 
* ><encrypt><![cdata[rypevhkd8qqkfhvq6qleeb4j58tipdvo 
* +rtk1i9qca6am/wvqnlsv5zepeusuix5l5x/0lwfrf0qadhhhgd3qczcdcupj911l3vg3w/ 
* syyvujts3tuuksuxxaccas0qhxchrryt66wispglyl42am6a8dtt 
* +6k4asknmpj48kzjs8qljvd4xgpue06dodnlxauhzm6 
* +kdz+hmzfjyur+ltwgc2hgf5gsijff0ekunxziqatp7pf5mzxz3izoun1s4zg4lumnvw2r 
* +kqckiw 
* +3iqh03v+bca9nmelnqbsf6tiwsrxjb3lavgucallcrw8v2t9el4ehzjwrquax5wlvmns0 
* +rupa3k22ncx4xxzs9o0mbh27bo6bpnelzps 
* +/uh9ksnly6bhcmju9p8g7m3fvkn28h3kdya5pl 
* /t8z1ptdave0lxdq2yoyyh2uypighbzzis2pdbs8r07+qn+e7q==]]></encrypt> 
* <agentid><![cdata[218]]></agentid> </xml> 
* 
* 企业收到post请求之后应该 
* 1.解析出url上的参数,包括消息体签名(msg_signature),时间戳(timestamp)以及随机数字串(nonce) 
* 2.验证消息体签名的正确性。 
* 3.将post请求的数据进行xml解析,并将<encrypt>标签的内容进行解密,解密出来的明文即是用户回复消息的明文,明文格式请参考官方文档 
* 第2,3步可以用公众平台提供的库函数decryptmsg来实现。 
*/ 
@post 
@path("station") 
public string receivemsg(string reqdata) { 
string msgsignature = request.getparameter("msg_signature"); 
string timestamp = request.getparameter("timestamp"); 
string nonce = request.getparameter("nonce"); 
// post请求的密文数据 
// string sreqdata = 
// "<xml><tousername><![cdata[wx5823bf96d3bd56c7]]></tousername><encrypt><![cdata[rypevhkd8qqkfhvq6qleeb4j58tipdvo+rtk1i9qca6am/wvqnlsv5zepeusuix5l5x/0lwfrf0qadhhhgd3qczcdcupj911l3vg3w/syyvujts3tuuksuxxaccas0qhxchrryt66wispglyl42am6a8dtt+6k4asknmpj48kzjs8qljvd4xgpue06dodnlxauhzm6+kdz+hmzfjyur+ltwgc2hgf5gsijff0ekunxziqatp7pf5mzxz3izoun1s4zg4lumnvw2r+kqckiw+3iqh03v+bca9nmelnqbsf6tiwsrxjb3lavgucallcrw8v2t9el4ehzjwrquax5wlvmns0+rupa3k22ncx4xxzs9o0mbh27bo6bpnelzps+/uh9ksnly6bhcmju9p8g7m3fvkn28h3kdya5pl/t8z1ptdave0lxdq2yoyyh2uypighbzzis2pdbs8r07+qn+e7q==]]></encrypt><agentid><![cdata[218]]></agentid></xml>"; 
try { 
string smsg = wxcpt.decryptmsg(msgsignature, timestamp, nonce, 
reqdata); 
// 解析出明文xml标签的内容进行处理 
documentbuilderfactory dbf = documentbuilderfactory.newinstance(); 
documentbuilder db = dbf.newdocumentbuilder(); 
stringreader sr = new stringreader(smsg); 
inputsource is = new inputsource(sr); 
document document = db.parse(is); 
element root = document.getdocumentelement(); 
nodelist nodelist1 = root.getelementsbytagname("content"); 
if (nodelist1.item(0) == null) 
return "ok"; 
string content = nodelist1.item(0).gettextcontent(); 
system.out.println("content:" + content); 
} catch (exception e) { 
e.printstacktrace();// 解密失败,失败原因请查看异常 
} 
return "ok"; 
} 
/* 
* ------------使用示例三:企业回复用户消息的加密--------------- 
* 企业被动回复用户的消息也需要进行加密,并且拼接成密文格式的xml串。 假设企业需要回复用户的明文如下: <xml> 
* <tousername><![cdata[mycreate]]></tousername> 
* <fromusername><![cdata[wx5823bf96d3bd56c7]]></fromusername> 
* <createtime>1348831860</createtime> <msgtype><![cdata[text]]></msgtype> 
* <content><![cdata[this is a test]]></content> 
* <msgid>1234567890123456</msgid> <agentid>128</agentid> </xml> 
* 
* 为了将此段明文回复给用户,企业应: 
* 1.自己生成时间时间戳(timestamp),随机数字串(nonce)以便生成消息体签名,也可以直接用从公众平台的post 
* url上解析出的对应值。 2.将明文加密得到密文。 
* 3.用密文,步骤1生成的timestamp,nonce和企业在公众平台设定的token生成消息体签名。 
* 4.将密文,消息体签名,时间戳,随机数字串拼接成xml格式的字符串,发送给企业。 
* 以上2,3,4步可以用公众平台提供的库函数encryptmsg来实现。 
*/ 
// @get 
// @path("send") 
public void sendmsg(string timestamp, string nonce) { 
string srespdata = "<xml><tousername><![cdata[mycreate]]></tousername><fromusername><![cdata[wxe49318eb604cf00b]]></fromusername><createtime>1348831860</createtime><msgtype><![cdata[text]]></msgtype><content><![cdata[this is a test]]></content><msgid>1234567890123456</msgid><agentid>1</agentid></xml>"; 
try { 
string sencryptmsg = wxcpt.encryptmsg(srespdata, timestamp, nonce); 
system.out.println("after encrypt sencrytmsg: " + sencryptmsg); 
response.getwriter().print(sencryptmsg); 
} catch (exception e) { 
e.printstacktrace();// 加密失败 
} 
// return srespdata; 
} 
/** 
* 此方法可以发送任意类型消息 
* 
* @param msgtype 
* text|image|voice|video|file|news 
* @param touser 
* 成员id列表(消息接收者,多个接收者用‘|'分隔,最多支持1000个)。特殊情况:指定为@all, 
* 则向关注该企业应用的全部成员发送 
* @param toparty 
* 部门id列表,多个接收者用‘|'分隔,最多支持100个。当touser为@all时忽略本参数 
* @param totag 
* 标签id列表,多个接收者用‘|'分隔。当touser为@all时忽略本参数 
* @param content 
* msgtype=text时 ,文本消息内容 
* @param mediaid 
* msgtype=image|voice|video时 ,对应消息信息id(--------) 
* @param title 
* msgtype=news|video时,消息标题 
* @param description 
* msgtype=news|video时,消息描述 
* @param url 
* msgtype=news时,消息链接 
* @param picurl 
* msgtype=news时,图片路径 
* @param safe 
* 表示是否是保密消息,0表示否,1表示是,默认0 
*/ 
public void sendwechatmsg(string msgtype, string touser, string toparty, 
string totag, string content, string mediaid, string title, 
string description, string url, string picurl, string safe) { 
url url; 
string access_token = getaccesstoken(); 
// 拼接请求串 
string action = create_session_url + access_token; 
// 封装发送消息请求json 
stringbuffer sb = new stringbuffer(); 
sb.append("{"); 
sb.append("\"touser\":" + "\"" + touser + "\","); 
sb.append("\"toparty\":" + "\"" + toparty + "\","); 
sb.append("\"totag\":" + "\"" + totag + "\","); 
if (msgtype.equals("text")) { 
sb.append("\"msgtype\":" + "\"" + msgtype + "\","); 
sb.append("\"text\":" + "{"); 
sb.append("\"content\":" + "\"" + content + "\""); 
sb.append("}"); 
} else if (msgtype.equals("image")) { 
sb.append("\"msgtype\":" + "\"" + msgtype + "\","); 
sb.append("\"image\":" + "{"); 
sb.append("\"media_id\":" + "\"" + mediaid + "\""); 
sb.append("}"); 
} else if (msgtype.equals("voice")) { 
sb.append("\"msgtype\":" + "\"" + msgtype + "\","); 
sb.append("\"voice\":" + "{"); 
sb.append("\"media_id\":" + "\"" + mediaid + "\""); 
sb.append("}"); 
} else if (msgtype.equals("video")) { 
sb.append("\"msgtype\":" + "\"" + msgtype + "\","); 
sb.append("\"video\":" + "{"); 
sb.append("\"media_id\":" + "\"" + mediaid + "\","); 
sb.append("\"title\":" + "\"" + title + "\","); 
sb.append("\"description\":" + "\"" + description + "\""); 
sb.append("}"); 
} else if (msgtype.equals("file")) { 
sb.append("\"msgtype\":" + "\"" + msgtype + "\","); 
sb.append("\"file\":" + "{"); 
sb.append("\"media_id\":" + "\"" + mediaid + "\""); 
sb.append("}"); 
} else if (msgtype.equals("news")) { 
sb.append("\"msgtype\":" + "\"" + msgtype + "\","); 
sb.append("\"news\":" + "{"); 
sb.append("\"articles\":" + "["); 
sb.append("{"); 
sb.append("\"title\":" + "\"" + title + "\","); 
sb.append("\"description\":" + "\"" + description + "\","); 
sb.append("\"url\":" + "\"" + url + "\","); 
sb.append("\"picurl\":" + "\"" + picurl + "\""); 
sb.append("}"); 
sb.append("]"); 
sb.append("}"); 
} 
sb.append(",\"safe\":" + "\"" + safe + "\","); 
sb.append("\"agentid\":" + "\"" + agentid + "\","); 
sb.append("\"debug\":" + "\"" + "1" + "\""); 
sb.append("}"); 
string json = sb.tostring(); 
try { 
url = new url(action); 
httpsurlconnection http = (httpsurlconnection) url.openconnection(); 
http.setrequestmethod("post"); 
http.setrequestproperty("content-type", 
"application/json;charset=utf-8"); 
http.setdooutput(true); 
http.setdoinput(true); 
system.setproperty("sun.net.client.defaultconnecttimeout", "30000");// 
// 连接超时30秒 
system.setproperty("sun.net.client.defaultreadtimeout", "30000"); // 
// 读取超时30秒 
http.connect(); 
outputstream os = http.getoutputstream(); 
os.write(json.getbytes("utf-8"));// 传入参数 
inputstream is = http.getinputstream(); 
int size = is.available(); 
byte[] jsonbytes = new byte[size]; 
is.read(jsonbytes); 
string result = new string(jsonbytes, "utf-8"); 
system.out.println("请求返回结果:" + result); 
os.flush(); 
os.close(); 
} catch (exception e) { 
e.printstacktrace(); 
} 
} 
public static void main(string[] args) { 
stationresource wechat = new stationresource(); 
// wechat.sendwechatmsgtext("@all", "2", "", "信息中心通知", "0"); 
wechat.sendwechatmsg("news", "@all", "", "", "测试senmsg", "", "测试的", 
"真的是测试的", "http://www.baidu.com", 
"http://file27.mafengwo.net/m00/b2/12/wkgb6lo0ahwamhl8aav1ybfjdjw20.jpeg", "0"); 
} 
}

以上所述是小编给大家介绍的微信企业号验证/发送/接收消息,希望对大家有所帮助