起步软件技术论坛
搜索
 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1724|回复: 1

[结贴] 微信企业号的servlet服务端,运行测试报错!

[复制链接]

31

主题

96

帖子

397

积分

中级会员

Rank: 3Rank: 3

积分
397
QQ
发表于 2015-6-15 10:47:31 | 显示全部楼层 |阅读模式
测试运行不能显示“非法请求”
报错如下:
[Fatal Error] :1:1: 文件提前结束。
六月 12, 2015 4:11:52 下午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet wxproservlet threw exception
java.lang.RuntimeException: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; 文件提前结束。
        at me.chanjar.weixin.common.util.crypto.WxCryptUtil.extractEncryptPart(WxCryptUtil.java:299)
        at me.chanjar.weixin.common.util.crypto.WxCryptUtil.decrypt(WxCryptUtil.java:160)
        at me.chanjar.weixin.cp.bean.WxCpXmlMessage.fromEncryptedXml(WxCpXmlMessage.java:423)
        at me.chanjar.weixin.cp.bean.WxCpXmlMessage.fromEncryptedXml(WxCpXmlMessage.java:432)
        at wxpro.wxproservlet.service(wxproservlet.java:90)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
        at java.lang.Thread.run(Thread.java:745)
Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; 文件提前结束。
        at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:257)
        at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:347)
        at me.chanjar.weixin.common.util.crypto.WxCryptUtil.extractEncryptPart(WxCryptUtil.java:294)
        ... 17 more

程序的weixin-java-tools 是1.0.6
程序源代码:
package wxpro;

import java.util.*;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import me.chanjar.weixin.common.util.StringUtils;
//import me.chanjar.weixin.cp.api.WxCpConfigStorage;
import me.chanjar.weixin.cp.api.WxCpInMemoryConfigStorage;
import me.chanjar.weixin.cp.api.WxCpMessageHandler;
import me.chanjar.weixin.cp.api.WxCpMessageRouter;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.WxCpServiceImpl;
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
import me.chanjar.weixin.cp.bean.WxCpXmlOutTextMessage;
import me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil;

/**
* Servlet implementation class wxproservlet
*/
public class wxproservlet extends HttpServlet {
        private static final long serialVersionUID = 10L;

        protected WxCpInMemoryConfigStorage wxCpConfigStorage;
          protected WxCpService wxCpService;
          protected WxCpMessageRouter wxCpMessageRouter;

          @Override public void init() throws ServletException {
            super.init();

            wxCpConfigStorage = new WxCpInMemoryConfigStorage();
            wxCpConfigStorage.setCorpId("wx5f2b66dea8f30a5c");      // 设置微信企业号的appid
            wxCpConfigStorage.setCorpSecret("xQ0MClBI9idRRUBj9y-AXToR1L77GqcqbYGkqOiqLQ_468QOxLU-M9D98KuhFXtX");  // 设置微信企业号的app corpSecret
            wxCpConfigStorage.setAgentId("0");     // 设置微信企业号应用ID
            wxCpConfigStorage.setToken("qwOfmKkRjXX2pjMezZSuFgpoDVbcPf");       // 设置微信企业号应用的token
            //wxCpConfigStorage.setAesKey("YPstbnxqJaRcDZbRPwE7xRPIzzECYIir3GsA1X5BniB");      // 设置微信企业号应用的EncodingAESKey

            wxCpService = new WxCpServiceImpl();
            wxCpService.setWxCpConfigStorage(wxCpConfigStorage);

            WxCpMessageHandler handler = new WxCpMessageHandler() {
              @Override public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService wxCpService) {
                WxCpXmlOutTextMessage m = WxCpXmlOutMessage
                        .TEXT()
                        .content("测试加密消息")
                        .fromUser(wxMessage.getToUserName())
                        .toUser(wxMessage.getFromUserName())
                    .build();
                return m;
              }
            };

            wxCpMessageRouter = new WxCpMessageRouter(wxCpService);
            wxCpMessageRouter
                .rule()
                .async(false)
                .content("哈哈") // 拦截内容为“哈哈”的消息
                .handler(handler)
                .end();
          }

          @Override protected void service(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {

            response.setContentType("text/html;charset=utf-8");
            response.setStatus(HttpServletResponse.SC_OK);

            String msgSignature = request.getParameter("msg_signature");
            String nonce = request.getParameter("nonce");
            String timestamp = request.getParameter("timestamp");
            String echostr = request.getParameter("echostr");

            if (StringUtils.isNotBlank(echostr)) {
              if (!wxCpService.checkSignature(msgSignature, timestamp, nonce, echostr)) {
                // 消息签名不正确,说明不是公众平台发过来的消息
                response.getWriter().println("非法请求");
                return;
              }
              WxCpCryptUtil cryptUtil = new WxCpCryptUtil(wxCpConfigStorage);
              String plainText = cryptUtil.decrypt(echostr);
              // 说明是一个仅仅用来验证的请求,回显echostr
              response.getWriter().println(plainText);
              return;
            }

            WxCpXmlMessage inMessage = WxCpXmlMessage.fromEncryptedXml(request.getInputStream(), wxCpConfigStorage, timestamp, nonce, msgSignature);
            WxCpXmlOutMessage outMessage = wxCpMessageRouter.route(inMessage);
            if (outMessage != null) {
              response.getWriter().write(outMessage.toEncryptedXml(wxCpConfigStorage));
            }

            return;
          }

}

91

主题

13万

帖子

3万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
35920
发表于 2015-6-15 16:11:35 | 显示全部楼层
同样的问题请不要重复发帖

http://bbs.wex5.com/forum.php?mod=viewthread&tid=73517
远程的联系方法QQ1392416607,添加好友时,需在备注里注明其论坛名字及ID,公司等信息
发远程时同时也发一下帖子地址,方便了解要解决的问题  WeX5教程  WeX5下载



如按照该方法解决,请及时跟帖,便于版主结贴
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|X3技术论坛|Justep Inc.    

GMT+8, 2024-5-2 22:54 , Processed in 0.095275 second(s), 23 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表