如何在 Eclipse 中针对 HTTPS url 修复 "Connection Reset"

How to fix "Connection Reset" in Eclipse for HTTPS url

我正在尝试解析 https url link 并在 eclipse 中遇到 connection reset 问题,但是它在 Eclipse 中工作(尽管 returns 301 响应代码) 如何解决这个问题? 谢谢

try {

String url = "http://api.demo.globalgatewaye4.firstdata.com/transaction/search?start_date=2018-01-07&end_date=2018-01-09";

URL obj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setReadTimeout(5000);
conn.addRequestProperty("Authorization", "Basic *****************");


System.out.println("Request URL ... " + url);

boolean redirect = false;

// normally, 3xx is redirect
int status = conn.getResponseCode();
if (status != HttpURLConnection.HTTP_OK) {
    if (status == HttpURLConnection.HTTP_MOVED_TEMP
        || status == HttpURLConnection.HTTP_MOVED_PERM
            || status == HttpURLConnection.HTTP_SEE_OTHER)
    redirect = true;
}

System.out.println("Response Code ... " + status);

if (redirect) {

    // get redirect url from "location" header field
    String newUrl = conn.getHeaderField("Location");

    // get the cookie if need, for login
    String cookies = conn.getHeaderField("Set-Cookie");

    // open the new connnection again
    conn = (HttpURLConnection) new URL(newUrl).openConnection();
    conn.setRequestProperty("Cookie", cookies);
    conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
    conn.addRequestProperty("User-Agent", "Mozilla");
    conn.addRequestProperty("Referer", "google.com");

    System.out.println("Redirect to URL : " + newUrl);

}

BufferedReader in = new BufferedReader(
                          new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer html = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
    html.append(inputLine);
}
in.close();

System.out.println("URL Content... \n" + html.toString());
System.out.println("Done");

} catch (Exception e) {
e.printStackTrace();
}

这 returns 一个 HTTP 响应代码“301”,将其重定向到一个 HTTPS url,这又会导致连接重置错误。

如何在eclipse中解决这个错误,或者有没有通用的方法。

谢谢。找到了解决方案。问题与 TLS 在我使用的 java 版本中不可用有关。 不得不切换到更高的 java 版本。