为什么不断收到java.net.SocketException:连接重置错误?

问题描述 投票:0回答:1

我正在运行一个Java类GetURL.java,该类接受一个URL并返回该页面的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类是:

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地址正常工作

感谢您的协助

java httpurlconnection socketexception connection-reset
1个回答
0
投票

我有一个遇到这样问题的同事。

他发现这是因为他正在呼叫的服务器已删除了对TLS 1.0的支持,但Java 7默认使用TLS 1.0。

他通过强制Java使用TLS 1.2来修复它。

java -Dhttps.protocols=TLSv1.2 GetURL https://artifactory.somewhere.com/artifactory/webapp

© www.soinside.com 2019 - 2024. All rights reserved.