我正在尝试使用一些外部 API 来获取一些数据。当数据量很小时,一切正常,但是当 API 返回的数据量很大时,我得到CONNECTION RESET异常。下面的代码来自java类InterfaceHelper,我还在我得到异常的第no行标记了注释[尝试从InputStream读取数据时]。
我试图在 STACKOVERFLOW 本身上搜索这么多问题,但没有找到合适的答案。所以请不要将其标记为重复问题。我想知道这个问题背后的原因是什么,以及这个问题的正确解决方案是什么。如果您发现此问题重复,请在将其标记为重复之前回答。我赌你。
在下面找到我使用过的代码。URL 是一些虚拟 url,出于安全原因,我不能提及我使用的实际 URL。
try{
URL url = new URL("http://example.com/someParams/SOME-ACCESS-TOKEN");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Language", "en-US");
connection.setRequestProperty("X-EXAMPLE-LOGIN", "XXXXXXXX");
connection.setRequestProperty("X-EXAMPLE-PASSWORD", "XXXXXX");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
DataInputStream input = new DataInputStream(connection.getInputStream());
String ret = "";
if(input!=null){
for( int c = input.read(); c != -1; c = input.read() ) { //InterfaceHelper.java:695
ret = ret + String.valueOf((char)c);
}
}
if(input!=null)
input.close();
if(connection!=null)
connection.disconnect();
if(ret!=null && ret.length()>0){
return ret;
}
}catch(Exception e) {
e.printStackTrace();
}
这是我得到的例外
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:196)
at java.net.SocketInputStream.read(SocketInputStream.java:122)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:442)
at sun.security.ssl.InputRecord.readV3Record(InputRecord.java:554)
at sun.security.ssl.InputRecord.read(InputRecord.java:509)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:927)
at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:884)
at sun.security.ssl.AppInputStream.read(AppInputStream.java:102)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:275)
at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
at sun.net.www.http.ChunkedInputStream.fastRead(ChunkedInputStream.java:244)
at sun.net.www.http.ChunkedInputStream.read(ChunkedInputStream.java:689)
at java.io.FilterInputStream.read(FilterInputStream.java:133)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:3053)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:3047)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:3035)
at java.io.FilterInputStream.read(FilterInputStream.java:83)
at com.lsa.akosha.util.InterfaceHelper.hitApiBrandWatch(InterfaceHelper.java:695)
at com.lsa.akosha.service.brand.BrandCronService.brandSentiments(BrandCronService.java:288)
at com.lsa.akosha.util.thread.BrandWatchCronConverse.executeInternal(BrandWatchCronConverse.java:60)
at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:113)
at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:557)