java 尝试使用 HTTPS 连接:服务器重定向次数过多
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14392120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tring to connect using HTTPS: Server redirected too many times
提问by vamsi krishna
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.comin browser by disabling proxy settings. Also, we have installed certificates in trusted root certificates. I have added these certificates to Java Keystore successfully.
我正在尝试使用 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();
}
}
}
I have encountered the following error:
我遇到了以下错误:
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?
任何人都可以帮我解决这个例外吗?
回答by THIHA SOE
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.
如果您想检查站点的可用性,您应该使用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();
}
}
}