在Spring Rest Template中出现java.net.SocketException: Connection reset错误

11
我这里有一个奇怪的问题。
以下代码在客户端上重新启动tomcat服务器之前都能很好地运行。一旦我使用相同代码的最新war文件重新启动tomcat服务器(客户端程序位于war文件中),就会抛出以下错误。我正在使用JDK 8。
以下是示例代码。通过浏览器,我能从下面程序中使用的URL获得响应。但是无法使用java程序或Postman获取数据。
package com.example.demo;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class TestService implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        download();
    }
    
    private void download() {
        System.out.println("Started download");
        try {
            RestTemplate restTemplate = new RestTemplate();
            String url = "https://www.nseindia.com/live_market/dynaContent/live_watch/stock_watch/niftyStockWatch.json";
            byte[] forObject = restTemplate.getForObject(url, byte [].class);
            System.out.println(forObject);
            System.out.println("Downloaded");
        } catch (Exception e) {
            System.out.println("Exception " + e);
        }
    }
}

以下是引发的异常:

org.springframework.web.client.ResourceAccessException: I/O error on GET request for "https://www.nseindia.com/live_market/dynaContent/live_watch/stock_watch/niftyStockWatch.json": Connection reset; nested exception is java.net.SocketException: Connection reset
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:751)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:677)
    at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:345)
    at com.sudhasoft.service.impl.StocksServiceImpl.getNiftyData(StocksServiceImpl.java:183)
    at com.sudhasoft.service.impl.StocksServiceImpl.getNifty500Data(StocksServiceImpl.java:154)
    at com.sudhasoft.service.impl.PatternServiceImpl.getDataBySignal(PatternServiceImpl.java:444)
    at com.sudhasoft.service.impl.PatternServiceImpl.loadDataOnInit(PatternServiceImpl.java:1090)
    at com.sudhasoft.service.CacheServiceImpl.initCache(CacheServiceImpl.java:29)
    at com.sudhasoft.scheduler.job.CacheJob.clearCache(CacheJob.java:41)
    at com.sudhasoft.scheduler.job.CacheJob.executeJob(CacheJob.java:25)
    at com.sudhasoft.scheduler.StockScheduler$1.run(StockScheduler.java:120)
Caused by: java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at sun.security.ssl.InputRecord.readFully(Unknown Source)
    at sun.security.ssl.InputRecord.read(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readDataRecord(Unknown Source)
    at sun.security.ssl.AppInputStream.read(Unknown Source)
    at okio.Okio$2.read(Okio.java:139)
    at okio.AsyncTimeout$2.read(AsyncTimeout.java:237)
    at okio.RealBufferedSource.indexOf(RealBufferedSource.java:345)
    at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:217)
    at okhttp3.internal.http1.Http1Codec.readHeaderLine(Http1Codec.java:212)
    at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189)
    at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
    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:125)
    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 org.springframework.http.client.OkHttp3ClientHttpRequest.executeInternal(OkHttp3ClientHttpRequest.java:73)
    at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
    at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:742)
    ... 10 common frames omitted

2
如果我们可以忽略这个问题是在重新启动服务器(或客户端?)之后才出现的,那么这将是一个重复的问题,类似于https://dev59.com/tW035IYBdhLWcg3wSOCr或https://stackoverflow.com/questions/37005352/java-net-socketexception-connection-reset-when-running-a-simplessl-client,即客户端和服务器使用不兼容的SSL设置... - Petr Bodnár
看起来你的实际代码与这里提供的示例代码不同。如果不查看实际代码,就无法找到根本原因。 - DinushaNT
你有试过跳过 SSL 验证吗? - Keaz
请问您能否发布您的POM.XML文件? - Varun Jain
3个回答

0

这看起来像是代理相关的错误。

您需要提供代理信息才能连接到该URL。

它在浏览器中工作是因为可能已经配置了互联网代理,但需要在代码级别显式提供才能连接到互联网。

Postman也是如此。您必须在Postman配置下提供代理服务器信息。


0

最简单的答案是当您重新启动Tomcat时,您只需关闭套接字,然后尝试向已关闭的套接字发送更多数据。这就是为什么您会收到IO异常,因为您无法写入数据。


0

这似乎是由于防火墙问题引起的。

为了解决这个问题,您需要使用代理服务器。您可以使用以下两行代码添加代理服务器和端口:

System.setProperty("proxyHost", "proxyServer.proxy.com");
System.setProperty("proxyPort", "9801");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接