【问题标题】:Tring to connect using HTTPS: Server redirected too many times尝试使用 HTTPS 进行连接:服务器重定向太多次
【发布时间】:2013-01-01 18:17:00
【问题描述】:

我正在尝试使用 Java 程序连接到安全连接 URL (https://example.com) 以检查站点的可用性。通常,我通过禁用代理设置在浏览器中连接到https://example.com。此外,我们在受信任的根证书中安装了证书。 我已成功将这些证书添加到 Java Keystore。

  import java.net.URL;
    import java.net.URLConnection;
    import java.security.Security.*;
    import com.sun.net.ssl.*;
    import com.sun.*; 

    import javax.net.ssl.HttpsURLConnection;
    import java.security.cert.Certificate;
    import java.io.*;
    import javax.net.ssl.SSLPeerUnverifiedException;
    import org.omg.CORBA_2_3.portable.InputStream;

    public class TestConn {
    public static void main(String args [])
    {
        try{
                 URL hp = new URL("https://example.com");
                HttpsURLConnection hpCon = (HttpsURLConnection)hp.openConnection();

        boolean isProxy = hpCon.usingProxy();
        System.out.println("is using proxy " + isProxy);
        InputStream obj = (InputStream) hpCon.getInputStream();
        while(obj.read()!=-1){
            System.out.println(obj.read_char());

        }
                System.out.println("content >> " + obj.toString());
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
    }

我遇到了以下错误:

java.net.ProtocolException: Server redirected too many  times (20)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
    at TestConn.main(TestConn.java:28)

谁能帮我解决这个异常?

【问题讨论】:

标签: java https


【解决方案1】:

如果您想检查网站的可用性,您应该使用hpCon.getResponseCode();。 响应代码200 表示该站点可用。坦率地说,我不知道你的进一步目的。 这是修改后的代码,肯定得到了输出内容。

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;

public class Test {
public static void main(String[] args) {

    try {
        URL hp = new URL("https://godaddy.com");
        HttpsURLConnection hpCon = (HttpsURLConnection) hp.openConnection();

        boolean isProxy = hpCon.usingProxy();
        System.out.println("is using proxy " + isProxy);
        InputStream obj = hpCon.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(obj));

        String s;
        while ((s = br.readLine()) != null) {
            System.out.println("content >>" + s);
        }

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

【讨论】:

  • 感谢您的回复。我的目标是找到特定站点已启动并运行良好,或者服务器是否已关闭而没有通过浏览器实际打开该站点。我们正在使用 HTTPS 连接来连接站点并检查输出内容。但是,问题是我们可以通过浏览器访问该站点,但无法使用 Java 进行连接。我在做同样的事情时遇到了上述问题。
  • 到目前为止,您的目标是输出内容。让我对代码进行一些修改。 'java.io.InputStream obj = conn.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(obj));字符串 s; while((s= br.readLine())!= null){ System.out.println(s); }'
猜你喜欢
  • 2018-11-22
  • 2013-09-21
  • 2023-03-25
  • 2015-02-16
  • 2017-04-19
  • 2015-12-11
  • 2017-07-16
  • 2019-02-21
  • 1970-01-01
相关资源
最近更新 更多