我有一个使用HttpUrlConnection调用远程客户端服务的函数。这个函数工作正常,直到我们添加了一个新的安全远程客户端,只能使用HttpsUrlConnection调用。
调用新的https远程客户端工作正常,在调用新客户端后,旧客户端在尝试获取InputStream对象时将返回java.net.SocketException: Connection reset
。
我的代码看起来像这样:
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
conn.setRequestMethod("POST");
out = conn.getOutputStream();
writer = new OutputStreamWriter(out);
writer.write(strJson);
writer.close();
in = conn.getInputStream(); // connection reset error here.
reader = new InputStreamReader(in);
rd = new BufferedReader(reader);
while ((line = rd.readLine()) != null) {
result += line;
}
rd.close();
in.close();
reader.close();
out.close();
conn.disconnect();
任何人都知道发生了什么? 提前谢谢。
堆栈跟踪如下:
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.read(InputRecord.java:480)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:934)
at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:891)
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.HttpClient.parseHTTPHeader(HttpClient.java:690)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:633)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:661)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1324)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
答案 0 :(得分:0)
我仍然不知道导致错误发生的原因。我的解决方案是不使用java.net.HttpURLConnection,而是使用org.apache.http.impl.client.DefaultHttpClient。