Ich habe ein Problem beim Herstellen einer Verbindung zu einer HTTP-Anforderung für eine bestimmte URL in meinem Java-Code. Zum Beispiel: http://www.linkedin.com. Dies wirft Server nicht verfügbar Fehler. (TimeOutException). Aber, möchte ich meine Verbindung HTTP-Anforderung an Location-Header Wert umleiten, wenn Response 301 oder 302 istHTTP zu HTTPS Umleitung in Java mit HTTPURLConnection
Code Snippet:
HttpURLConnection urlConn =(HttpURLConnection) new URL(url).openConnection(); //For eg : http://www.linkedin.com
urlConn.setConnectTimeout(10*1000);
urlConn.setReadTimeout(10*1000);
boolean redirect = false;
int status = urlConn.getResponseCode(); //No response. Throws exception
if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM)
{
redirect = true;
}
String contenttype = urlConn.getContentType();
if(redirect)
{
String newUrl = urlConn.getHeaderField("Location");//No I18N
urlConn = (HttpURLConnection) new URL(newUrl).openConnection();
urlConn.connect();
contenttype = urlConn.getContentType();
}
Eine Timeout-Ausnahme weist nicht auf eine fehlgeschlagene Weiterleitung hin - es ist wahrscheinlicher, dass Sie eine Firewall treffen. HttpUrlConnection führt eine automatische Umleitung auf 30x Codes durch. Überprüfen Sie Ihr Netzwerk "netstat -na", während die Verbindung versucht, den SYN_SENT-Status zu ermitteln und zu suchen. – Jan
Wenn Sie in Ihrem Netzwerk einen Proxy implementiert haben, versuchen Sie dies mit SSL Context zu implementieren, wie in [this] (http://stackoverflow.com) beschrieben/questions/1511674/how-do-ein-send-an-https-request-durch-ein-proxy-in-java) post. – redoc