【发布时间】: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)
谁能帮我解决这个异常?
【问题讨论】:
-
那么
isProxy的值是多少?它是否声称您正在使用一个或没有?