小程序消息推送问题总结

微信小程序推送乱码问题

2018-09-08 今天在做微信消息推送时,消息内容乱码,如下图:

首先,想到的是idea的编码方式,在file–>setting–>editor–>file encodings下下拉框均选择utf-8

其次,想到的是string字符串转码问题,通过new String(str.getBytes(“gbk”),“UTF-8”);在微信上显示的变为了问号。???
然后,猜测会不会是在拼接url时中文编码问题,于是使用URLEncoder.encode(“内容”);在微信上显示的变为了各种百分号。。。。
最后,在百度上搜索“微信消息推送乱码”,参考:java微信 客服接口-发消息 中文乱码 成功解决问题

将原有代码 pw = new PrintWriter(connection.getOutputStream());
改为

OutputStreamWriter outWriter = 
new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
    pw = new PrintWriter(outWriter);

全部代码如下:

String result = "";
PrintWriter pw = null;
BufferedReader br = null;
try {
    URL realUrl = new URL(requestUrl);
    HttpsURLConnection connection = (HttpsURLConnection)realUrl.openConnection();
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setRequestMethod("POST");
    connection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
    // 获取URLConnection对象对应的输出流
    OutputStreamWriter outWriter = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
    pw = new PrintWriter(outWriter);
    // 发送请求参数
    pw.print(param);
    // flush输出流的缓冲
    pw.flush();
    // 建立实际的连接
    connection.connect();
    br = new BufferedReader(new InputStreamReader(
            connection.getInputStream()));
    String line;
    while ((line = br.readLine()) != null) {
        result += line;
    }
} catch (Exception e) {
    e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
    try {
        if (br != null) {
            br.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

微信小程序推送报错

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: timestamp check failed
        at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
        at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949)
        at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
        at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296)
        at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1509)
        at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
        at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
        at sun.security.ssl.Handshaker.process_record(Handshaker.java:914)
        at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)
        at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
        at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
        at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
        at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)
        at cn.wyc.web.wx.util.UserUtil.getResult(Util.java:62)
        at cn.wyc.web.wx.util.UserUtil.getToken(Util.java:39)
        at cn.wyc.web.wx.util.TokenUtil.run(TokenUtil.java:13)
        at java.lang.Thread.run(Thread.java:745)
Caused by: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: timestamp check failed
        at sun.security.validator.PKIXValidator.doValidate(PKIXValidator.java:352)
        at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:249)
        at sun.security.validator.Validator.validate(Validator.java:260)
        at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
        at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
        at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
        at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1491)
        ... 14 more
Caused by: java.security.cert.CertPathValidatorException: timestamp check failed
        at sun.security.provider.certpath.PKIXMasterCertPathValidator.validate(PKIXMasterCertPathValidator.java:135)
        at sun.security.provider.certpath.PKIXCertPathValidator.validate(PKIXCertPathValidator.java:219)
        at sun.security.provider.certpath.PKIXCertPathValidator.validate(PKIXCertPathValidator.java:140)
        at sun.security.provider.certpath.PKIXCertPathValidator.engineValidate(PKIXCertPathValidator.java:79)
        at java.security.cert.CertPathValidator.validate(CertPathValidator.java:292)
        at sun.security.validator.PKIXValidator.doValidate(PKIXValidator.java:347)
        ... 20 more
Caused by: java.security.cert.CertificateExpiredException: NotAfter: Fri Feb 15 20:00:00 HKT 2019
        at sun.security.x509.CertificateValidity.valid(CertificateValidity.java:274)
        at sun.security.x509.X509CertImpl.checkValidity(X509CertImpl.java:629)
        at sun.security.provider.certpath.BasicChecker.verifyTimestamp(BasicChecker.java:190)
        at sun.security.provider.certpath.BasicChecker.check(BasicChecker.java:144)
        at sun.security.provider.certpath.PKIXMasterCertPathValidator.validate(PKIXMasterCertPathValidator.java:125)
        ... 25 more
Exception in thread "Thread-11" java.lang.NullPointerException
        at cn.wyc.web.wx.util.UserUtil.getToken(Util.java:41)
        at cn.wyc.web.wx.util.TokenUtil.run(TokenUtil.java:13)
        at java.lang.Thread.run(Thread.java:745)

此错误表示验证失败及时间戳问题,在本地尝试多次没问题,发现是服务器时间的问题,显示为了2019年,修改时间解决问题

查看和修改Linux的时间1. 查看时间和日期
命令 : “date”

2.设置时间和日期
例如:将系统日期设定成2009年11月3日的命令
命令 : “date -s 11/03/2009”
将系统时间设定成下午5点55分55秒的命令
命令 : “date -s 17:55:55”

date
显示当前时间 Fri Aug 3 14:15:16 CST 2007

date -s
按字符串方式修改时间
可以只修改日期,不修改时间,输入: date -s 2007-08-03
只修改时间,输入:date -s 14:15:00
同时修改日期时间,注意要加双引号,日期与时间之间有一空格,输入:date -s “2007-08-03 14:15:00”

修改完后,需要的话可以输入:clock -w
把系统时间写入CMOS

  1. 将当前时间和日期写入BIOS,避免重启后失效

命令 : “hwclock -w”

你可能感兴趣的:(微信小程序)