微信小程序获取sessionkey,Connection reset

微信小程序运行一段时间就会报这个错,重启之后就好了,我看网上也有在微信论坛里面说这个事的但是没解决方案。。

搞不懂是怎么回事~

String code = getAttr("code");
code = code.trim();
if (!MiniProgramsCodeCache.containsKey(code)) {
    com.jfinal.weixin.sdk.api.ApiResult sessionKeyResult = WxaUserApi.getSessionKey(code);
    MiniProgramsCodeCache.put(code, sessionKeyResult);
}
java.lang.RuntimeException: java.net.SocketException: Connection reset
        at com.jfinal.weixin.sdk.utils.HttpUtils$OkHttp3Delegate.exec(HttpUtils.java:305)
        at com.jfinal.weixin.sdk.utils.HttpUtils$OkHttp3Delegate.get(HttpUtils.java:312)
        at com.jfinal.weixin.sdk.utils.HttpUtils.get(HttpUtils.java:32)
        at com.jfinal.wxaapp.api.WxaUserApi.getSessionKey(WxaUserApi.java:46)
        at com.***.controller.TrademarkController.search(TrademarkController.java:195)
        at sun.reflect.GeneratedMethodAccessor30.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at com.jfinal.aop.Invocation.invoke(Invocation.java:97)
        at com.***.interceptor.ExceptionInterceptor.intercept(ExceptionInterceptor.java:17)
        at com.jfinal.aop.Invocation.invoke(Invocation.java:91)
        at com.***.interceptor.PostParaInterceptor.intercept(PostParaInterceptor.java:50)
        at com.jfinal.aop.Invocation.invoke(Invocation.java:91)
        at com.***.interceptor.UserPowerInterceptor.intercept(UserPowerInterceptor.java:28)
        at com.jfinal.aop.Invocation.invoke(Invocation.java:91)
        at com.***.interceptor.LocalParaInterceptor.intercept(LocalParaInterceptor.java:10)
        at com.jfinal.aop.Invocation.invoke(Invocation.java:91)
        at com.jfinal.core.ActionHandler.handle(ActionHandler.java:89)
        at com.jfinal.core.JFinalFilter.doFilter(JFinalFilter.java:90)
        at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
        at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
        at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
        at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
        at io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:68)
        at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
        at io.undertow.servlet.handlers.RedirectDirHandler.handleRequest(RedirectDirHandler.java:68)
        at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:132)
        at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
        at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
        at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
        at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
        at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
        at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
        at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
        at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
        at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
        at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:269)
        at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:78)
        at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:133)
        at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:130)
        at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
        at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
        at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:249)
        at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:78)
        at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:99)
        at io.undertow.server.Connectors.executeRootHandler(Connectors.java:376)
        at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:830)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(SocketInputStream.java:210)
        at java.net.SocketInputStream.read(SocketInputStream.java:141)
        at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
        at sun.security.ssl.InputRecord.read(InputRecord.java:503)
        at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:983)
        at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1385)
        at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1413)
        at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1397)
        at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:302)
        at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:270)
        at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:162)
        at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:257)
        at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)
        at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)
        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
        at okhttp3.RealCall.execute(RealCall.java:77)
        at com.jfinal.weixin.sdk.utils.HttpUtils$OkHttp3Delegate.exec(HttpUtils.java:299)
        ... 49 more


评论区

JFinal

2020-07-10 16:38

引起该异常的原因有两个:

1,如果一端的Socket被关闭(或主动关闭,或因为异常退出而 引起的关闭),另一端仍发送数据,发送的第一个数据包引发该异常(Connect reset by peer)。

2,一端退出,但退出时并未关闭该连接,另一端如果在从连接中读数据则抛出该异常(Connection reset)。简单的说就是在连接断开后的读和写操作引起的。

参考这里:
https://www.cnblogs.com/insane-Mr-Li/p/11114578.html

hjk0205

2020-07-14 21:52

@JFinal 这个我是知道的,就是不知道什么情况下会导致socket关闭,我看微信小程序的社区里面有人反映过也有遇到这个问题,我就在想说是不是微信小程序那边有什么情况会导致socket关闭

JFinal

2020-07-14 22:35

@hjk0205 用户在使用小程序时,强制关闭退出就可以办到

andriod 手机在调出程序列表时,可以左右或者上下划动关闭应用

hjk0205

2020-07-15 14:03

@JFinal 这个接口不是服务端在请求微信小程序的服务端吗,应该和用户的小程序没有关系吧,也不会是用户退出小程序之后微信服务端就直接把我们服务端到微信服务端的请求断掉吧,这么做是有代价的,开发有代价而且也没什么必要吧

JFinal

2020-07-15 17:31

@hjk0205 只要是客户端在通信过程中主动断开连接就可以重现该问题了

服务端除了忽略这类异常以外,没有别的什么办法,毕竟客户端的事情服务端无法控制

热门反馈

扫码入社