为什么我不断收到 java.net.SocketException:连接重置错误?
Why do I keep getting a java.net.SocketException: Connection reset error?
我是 运行 一个 Java Class GetURL.java 需要一个 URL 和 returns HTML页面,但是在尝试特定 URL:
时我不断得到以下输出
Reading URL: https://artifactory.somewhere.com/artifactory/webapp
url.getHost: artifactory.somewhere.com
Socket Inet Address: artifactory.somewhere.com/XX.XXX.XX.XX
https://artifactory.somewhere.com/artifactory/webapp is reachable: false
HTTP URL Connection (huc) is using a proxy: false
Can not connect
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:208)
at java.net.SocketInputStream.read(SocketInputStream.java:134)
at com.ibm.jsse2.a.a(a.java:227)
at com.ibm.jsse2.a.a(a.java:269)
at com.ibm.jsse2.qc.a(qc.java:459)
at com.ibm.jsse2.qc.h(qc.java:275)
at com.ibm.jsse2.qc.a(qc.java:541)
at com.ibm.jsse2.qc.startHandshake(qc.java:89)
at com.ibm.net.ssl.www2.protocol.https.c.afterConnect(c.java:173)
at com.ibm.net.ssl.www2.protocol.https.d.connect(d.java:63)
at com.ibm.net.ssl.www2.protocol.https.b.connect(b.java:96)
at GetURL.getURL(GetURL.java:45)
at GetURL.main(GetURL.java:78)
Java Class 是:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.net.Socket;
/**
* Chapter 2 Example
*
* This program uses the standard Java URL class to open a connection to a web
* page and download the contents.
*
* @author Jeff Heaton
* @version 1.0
*/
public class GetURL {
/**
* This method will display the URL specified by the parameter.
*
* @param u
* The URL to display.
*/
static protected void getURL(String u) {
URL url;
InputStream is;
InputStreamReader isr;
BufferedReader r;
String str;
try {
System.out.println("Reading URL: " + u);
url = new URL(u);
System.out.println("url.getHost: " + url.getHost());
Socket clientSocket = new Socket(url.getHost(), 443);
System.out.println("Socket Inet Address: " + clientSocket.getInetAddress());
Boolean canReachClient = clientSocket.getInetAddress().isReachable(5 * 1000);
System.out.println(u + " is reachable: " + canReachClient);
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
HttpURLConnection.setFollowRedirects(true);
huc.setConnectTimeout(5 * 1000);
huc.setRequestMethod("GET");
System.out.println("HTTP URL Connection (huc) is using a proxy: " + huc.usingProxy());
huc.connect(); // problem is HERE!!!!
is = huc.getInputStream();
isr = new InputStreamReader(is);
r = new BufferedReader(isr);
do {
str = r.readLine();
if (str != null)
System.out.println(str);
} while (str != null);
} catch (MalformedURLException e) {
System.out.println("Must enter a valid URL");
e.printStackTrace();
} catch (IOException e) {
System.out.println("Can not connect");
e.printStackTrace();
}
}
/**
* Program entry point.
*
* @param args
* Command line arguments. Specified the URL to download.
*/
static public void main(String args[]) {
if (args.length < 1)
System.out.println("Usage: GetURL [url]");
else
getURL(args[0]);
}
}
我已经设法确定错误发生在调用 huc.connect();
时,但无法找出原因。
我使用的是 jdk1.7。1_64-ibm 并且它可以按预期与其他 HTTPS 地址一起使用
提前感谢您的帮助
我有个同事 运行 遇到过这样的问题。
他发现这是因为他所调用的服务器已经删除了对 TLS 1.0 的支持,但是 java 7 默认使用 TLS 1.0。
他通过强制 java 使用 TLS 1.2 来修复它。
java -Dhttps.protocols=TLSv1.2 GetURL https://artifactory.somewhere.com/artifactory/webapp
我是 运行 一个 Java Class GetURL.java 需要一个 URL 和 returns HTML页面,但是在尝试特定 URL:
时我不断得到以下输出Reading URL: https://artifactory.somewhere.com/artifactory/webapp
url.getHost: artifactory.somewhere.com
Socket Inet Address: artifactory.somewhere.com/XX.XXX.XX.XX
https://artifactory.somewhere.com/artifactory/webapp is reachable: false
HTTP URL Connection (huc) is using a proxy: false
Can not connect
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:208)
at java.net.SocketInputStream.read(SocketInputStream.java:134)
at com.ibm.jsse2.a.a(a.java:227)
at com.ibm.jsse2.a.a(a.java:269)
at com.ibm.jsse2.qc.a(qc.java:459)
at com.ibm.jsse2.qc.h(qc.java:275)
at com.ibm.jsse2.qc.a(qc.java:541)
at com.ibm.jsse2.qc.startHandshake(qc.java:89)
at com.ibm.net.ssl.www2.protocol.https.c.afterConnect(c.java:173)
at com.ibm.net.ssl.www2.protocol.https.d.connect(d.java:63)
at com.ibm.net.ssl.www2.protocol.https.b.connect(b.java:96)
at GetURL.getURL(GetURL.java:45)
at GetURL.main(GetURL.java:78)
Java Class 是:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.net.Socket;
/**
* Chapter 2 Example
*
* This program uses the standard Java URL class to open a connection to a web
* page and download the contents.
*
* @author Jeff Heaton
* @version 1.0
*/
public class GetURL {
/**
* This method will display the URL specified by the parameter.
*
* @param u
* The URL to display.
*/
static protected void getURL(String u) {
URL url;
InputStream is;
InputStreamReader isr;
BufferedReader r;
String str;
try {
System.out.println("Reading URL: " + u);
url = new URL(u);
System.out.println("url.getHost: " + url.getHost());
Socket clientSocket = new Socket(url.getHost(), 443);
System.out.println("Socket Inet Address: " + clientSocket.getInetAddress());
Boolean canReachClient = clientSocket.getInetAddress().isReachable(5 * 1000);
System.out.println(u + " is reachable: " + canReachClient);
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
HttpURLConnection.setFollowRedirects(true);
huc.setConnectTimeout(5 * 1000);
huc.setRequestMethod("GET");
System.out.println("HTTP URL Connection (huc) is using a proxy: " + huc.usingProxy());
huc.connect(); // problem is HERE!!!!
is = huc.getInputStream();
isr = new InputStreamReader(is);
r = new BufferedReader(isr);
do {
str = r.readLine();
if (str != null)
System.out.println(str);
} while (str != null);
} catch (MalformedURLException e) {
System.out.println("Must enter a valid URL");
e.printStackTrace();
} catch (IOException e) {
System.out.println("Can not connect");
e.printStackTrace();
}
}
/**
* Program entry point.
*
* @param args
* Command line arguments. Specified the URL to download.
*/
static public void main(String args[]) {
if (args.length < 1)
System.out.println("Usage: GetURL [url]");
else
getURL(args[0]);
}
}
我已经设法确定错误发生在调用 huc.connect();
时,但无法找出原因。
我使用的是 jdk1.7。1_64-ibm 并且它可以按预期与其他 HTTPS 地址一起使用
提前感谢您的帮助
我有个同事 运行 遇到过这样的问题。
他发现这是因为他所调用的服务器已经删除了对 TLS 1.0 的支持,但是 java 7 默认使用 TLS 1.0。
他通过强制 java 使用 TLS 1.2 来修复它。
java -Dhttps.protocols=TLSv1.2 GetURL https://artifactory.somewhere.com/artifactory/webapp