JAVA微信开发教程入门篇 -01 开发环境搭建

来源:互联网 发布:淘宝佣金是多少 编辑:程序博客网 时间:2024/05/01 13:10
JAVA微信开发QQ群:211202664

 

相信很多人对微信开发已经不那么陌生,我也是从一个微信开发的菜鸟经过各种问题的折磨,然后去搜索引擎搜索各种文章阅读,但是基本都是零散的资料,没有一个统一、系统的阐述微信应用如何开发。作者结合自己的实际开发经验,归纳整理出来分享给微信开发的入门者或正在研究的开发者。

这篇文章主要阐述如何搭建一个微信开发平台。古人云:“磨刀不负砍才工”。我们开发应用也需要准备一些必要的条件。

 

Java开发环境搭建本文不在说明。

 Jdk1.6

 Tomcat 7.0

 Myeclipse 6.5

 

开发环境的搭建步骤:

 

1.      申请一个公网IP地址,发布自己的应用。

例如: 192.168.1.102

 

2.      打开myeclipse 创建一个工程

如下图所示:

  

 

 

 

创建一个微信应用接入Servlet类

 

 

 

源码:

 

package org.wx.servlet;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

import java.util.Arrays;

import java.util.logging.Logger;

 

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

 

import org.wx.service.WeixinEsb;

import org.wx.service.impl.WeixinEsbImpl;

import org.wx.util.StringUtil;

import org.wx.util.WoHelperCache;

import org.wx.util.WohelperConfig;

import org.wx.util.WohelperGUI;

 

 

/**

 *@authorhaibing.xiao

 *@sincejdk1.6

 *@version1.0

 */

publicclass WeixinServletextends HttpServlet{

 

    private Loggerlog =Logger.getLogger(this.getClass().getName());

    privatestaticfinallongserialVersionUID = 1L;

    private final StringToken="wohelper168";  //长度有限制,不能太长,接入微信平台用到。

    private  Stringechostr;

    @Override

    protectedvoid doGet(HttpServletRequest request, HttpServletResponseresponse)

           throws ServletException, IOException {

        connect(request,response);

    }

 

    @Override

    protectedvoid doPost(HttpServletRequest request, HttpServletResponseresponse)

           throws ServletException, IOException {

        

    }

 

     

   

    /**

     *@authorhaibing.xiao

     *@return 

     *@exception

     *@param

     *

     *<p>接入连接生效验证</p> 说明微信接入必须是get方法

     */

    privatevoid connect(HttpServletRequest request,HttpServletResponseresponse)throws ServletException, IOException{

       log.info("RemoteAddr: "+ request.getRemoteAddr());

       log.info("QueryString: "+ request.getQueryString());

        if(!accessing(request,response)){

            log.info("服务器接入失败.......");

            return ;

        }

       String echostr=getEchostr();

       if(echostr!=null && !"".equals(echostr)){

              log.info("服务器接入生效..........");

              response.getWriter().print(echostr);//完成相互认证

       }

    }

    /**

     *@authorhaibing.xiao

     *Date2013-05-29

     *@returnboolean

     *@exceptionServletException,IOException

     *@param

     *

     *<p>用来接收微信公众平台的验证</p>

     */

    privateboolean accessing(HttpServletRequest request,HttpServletResponse response)throwsServletException, IOException {

       String signature = request.getParameter("signature");

       String timestamp = request.getParameter("timestamp");

       String nonce = request.getParameter("nonce");

       String echostr = request.getParameter("echostr");

       if(StringUtil.isEmpty(signature)){

           returnfalse;

       }

       if(StringUtil.isEmpty(timestamp)){

           returnfalse;

       }

       if(StringUtil.isEmpty(nonce)){

           returnfalse;

       }

       if(StringUtil.isEmpty(echostr)){

           returnfalse;

       }

       String[] ArrTmp = { Token, timestamp, nonce };

       Arrays.sort(ArrTmp);

       StringBuffer sb = new StringBuffer();

       for (int i = 0; i < ArrTmp.length; i++) {

           sb.append(ArrTmp[i]);

       }

       String pwd = Encrypt(sb.toString());

        

       log.info("signature:"+signature+"timestamp:"+timestamp+"nonce:"+nonce+"pwd:"+pwd+"echostr:"+echostr);

       

        if(StringUtil.trim(pwd).equals(StringUtil.trim(signature))){

        this.echostr =echostr;

        returntrue;

        }else{

        returnfalse;

        }

    }

    private String Encrypt(String strSrc) {

       MessageDigest md = null;

       String strDes = null;

 

       byte[] bt = strSrc.getBytes();

       try {

           md = MessageDigest.getInstance("SHA-1");

           md.update(bt);

           strDes = bytes2Hex(md.digest());//to HexString

       catch (NoSuchAlgorithmException e) {

           System.out.println("Invalidalgorithm.");

           returnnull;

       }

       return strDes;

    }

 

    public String bytes2Hex(byte[] bts) {

       String des = "";

       String tmp = null;

       for (int i = 0; i < bts.length; i++) {

           tmp = (Integer.toHexString(bts[i] & 0xFF));

           if (tmp.length() == 1) {

              des += "0";

           }

           des += tmp;

       }

       return des;

    }

   

     

    public String getEchostr(){

       returnechostr;

    }

     

 

     

 

 ;

    staticpublic String AppId ;

    static public String AppSecret ;

    @Override

    publicvoid init()throws ServletException {

             AppId =””;申请账号成功微信提供的

             AppSecret=””;申请账号成功微信提供的

    }

     

   

}

 

最后一步:

 

将应用发布到tomcat上,设置访问的URL

例如:申请的ip/应用上下文/weixin

 

测试访问:

http://192.168.1.102/test/weixin

后台打印相关的信息。

 

证明Servlet正确。

 

接入指南请阅读下一篇

JAVA微信开发教程入门篇 -02应用接入指南

 

 

 

 如果觉得这篇文章对你有帮助,请不要吝啬你的鼠标,帮助下博主,
进博主开的程序员茶店:http://csxstea.taobao.com凑个人气。万分感谢。

 

0 0
原创粉丝点击