如何使用微信sdk java版

Python09

如何使用微信sdk java版,第1张

1.首先我们新建一个Java开发包WeiXinSDK

2.包路径:com.ansitech.weixin.sdk

测试的前提条件:

假如我的公众账号微信号为:vzhanqun

我的服务器地址为:http://www.vzhanqun.com/

下面我们需要新建一个URL的请求地址

我们新建一个Servlet来验证URL的真实性,具体接口参考

http://mp.weixin.qq.com/wiki/index.php?title=接入指南

3.新建com.ansitech.weixin.sdk.WeixinUrlFilter.java

这里我们主要是获取微信服务器法师的验证信息,具体验证代码如下

[java] view plain copy print?

package com.ansitech.weixin.sdk

import com.ansitech.weixin.sdk.util.SHA1

import java.io.IOException

import java.util.ArrayList

import java.util.Collections

import java.util.Comparator

import java.util.List

import javax.servlet.Filter

import javax.servlet.FilterChain

import javax.servlet.FilterConfig

import javax.servlet.ServletException

import javax.servlet.ServletRequest

import javax.servlet.ServletResponse

import javax.servlet.http.HttpServletRequest

import javax.servlet.http.HttpServletResponse

public class WeixinUrlFilter implements Filter {

//这个Token是给微信开发者接入时填的

//可以是任意英文字母或数字,长度为3-32字符

private static String Token = "vzhanqun1234567890"

@Override

public void init(FilterConfig config) throws ServletException {

System.out.println("WeixinUrlFilter启动成功!")

}

@Override

public void doFilter(ServletRequest req, ServletResponse res,

FilterChain chain) throws IOException, ServletException {

HttpServletRequest request = (HttpServletRequest) req

HttpServletResponse response = (HttpServletResponse) res

//微信服务器将发送GET请求到填写的URL上,这里需要判定是否为GET请求

boolean isGet = request.getMethod().toLowerCase().equals("get")

System.out.println("获得微信请求:" + request.getMethod() + " 方式")

if (isGet) {

//验证URL真实性

String signature = request.getParameter("signature")// 微信加密签名

String timestamp = request.getParameter("timestamp")// 时间戳

String nonce = request.getParameter("nonce")// 随机数

String echostr = request.getParameter("echostr")//随机字符串

List<String>params = new ArrayList<String>()

params.add(Token)

params.add(timestamp)

params.add(nonce)

//1. 将token、timestamp、nonce三个参数进行字典序排序

Collections.sort(params, new Comparator<String>() {

@Override

public int compare(String o1, String o2) {

return o1.compareTo(o2)

}

})

//2. 将三个参数字符串拼接成一个字符串进行sha1加密

String temp = SHA1.encode(params.get(0) + params.get(1) + params.get(2))

if (temp.equals(signature)) {

response.getWriter().write(echostr)

}

} else {

//处理接收消息

}

}

@Override

public void destroy() {

}

}

好了,不过这里有个SHA1算法,我这里也把SHA1算法的源码给贴出来吧!

4.新建com.ansitech.weixin.sdk.util.SHA1.java

[java] view plain copy print?

/*

* 微信公众平台(JAVA) SDK

*

* Copyright (c) 2014, Ansitech Network Technology Co.,Ltd All rights reserved.

* http://www.ansitech.com/weixin/sdk/

*

* Licensed under the Apache License, Version 2.0 (the "License")

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

* http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/

package com.ansitech.weixin.sdk.util

import java.security.MessageDigest

/**

* <p>Title: SHA1算法</p>

*

* @author [email protected]>

*/

public final class SHA1 {

private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5',

'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}

/**

* Takes the raw bytes from the digest and formats them correct.

*

* @param bytes the raw bytes from the digest.

* @return the formatted bytes.

*/

private static String getFormattedText(byte[] bytes) {

int len = bytes.length

StringBuilder buf = new StringBuilder(len * 2)

// 把密文转换成十六进制的字符串形式

for (int j = 0j <lenj++) {

buf.append(HEX_DIGITS[(bytes[j] >>4) &0x0f])

buf.append(HEX_DIGITS[bytes[j] &0x0f])

}

return buf.toString()

}

public static String encode(String str) {

if (str == null) {

return null

}

try {

MessageDigest messageDigest = MessageDigest.getInstance("SHA1")

messageDigest.update(str.getBytes())

return getFormattedText(messageDigest.digest())

} catch (Exception e) {

throw new RuntimeException(e)

}

}

}

5.把这个Servlet配置到web.xml中

[html] view plain copy print?

<filter>

<description>微信消息接入接口</description>

<filter-name>WeixinUrlFilter</filter-name>

<filter-class>com.ansitech.weixin.sdk.WeixinUrlFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>WeixinUrlFilter</filter-name>

<url-pattern>/api/vzhanqun</url-pattern>

</filter-mapping>

好了,接入的开发代码已经完成。

6.下面就把地址URL和密钥Token填入到微信申请成为开发者模式中吧。

微信公众平台有自己的java sdk的,微信公众平台也是一个开发平台,他们对第三方开发企业开放接口,让更多微信公众平台运营人员使用第三方的工具,如活动推送工具,可以对接【活动盒子】的java sdk,支付工具,客服工具这些的sdk也都可以对接。

1、就是开发工具包应该是可以用这个开发微信相关软件的。

2、微信开放SDK是采用SDK嵌入的方式,为第三方App提供一个与微信进行内容交换的通道,通过SDK的使用,第三方App可以实现分享信息给用户的微信好友和用户的微信朋友圈。