使用HTTPS进行连接:服务器重定向次数过多 [英] Tring to connect using HTTPS: Server redirected too many times

查看:607
本文介绍了使用HTTPS进行连接:服务器重定向次数过多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to connect to a secured connection URL (https://example.com ) using a Java program to check availability of the site. Generally, I connect to https://example.com in browser by disabling proxy settings. Also, we have installed certificates in trusted root certificates. I have added these certificates to Java Keystore successfully.

  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)

任何人都可以帮我解决这个例外吗?

Can anyone please help me regarding this exception?

推荐答案

如果要检查网站的可用性,则应使用 hpCon.getResponseCode();
回复代码 200 表示该网站可用。坦率地说,我不知道你的进一步目的。
这是修改过的代码,确实得到了输出内容。

If you want to check availability of the site, you should use hpCon.getResponseCode();. Response code 200 means that site is available. Frankly, i don't know your further purpose. This is the modified codes, sure got the output content.

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进行连接:服务器重定向次数过多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆