gpt4 book ai didi

java.io.IOException : Server returned HTTP response code: 403 for URL:unable to resolve

转载 作者:行者123 更新时间:2023-11-30 06:40:06 24 4
gpt4 key购买 nike

我面临以下异常:

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: https://xyz.abc.com

我已经尝试了许多论坛中提到的所有可能的解决方案,但仍然无法连接。

   try{
URLConnection openConnection = new URL("https://xyz.abc.com").openConnection();
openConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
InputStream is = openConnection.getInputStream();

InputSource source = new InputSource(is);

} catch (Exception e){
System.out.println("---ee---" + e);
throw e;
}

另一次试验:

 URL obj = new URL("https://xyz.abc.com");
HttpURLConnection connection = ((HttpURLConnection) obj.openConnection());
connection.addRequestProperty("User-Agent", "Mozilla/4.0");
System.out.println("--connection.getResponseCode() --" + connection.getResponseCode() );//403

任何建议都会有帮助。我需要安装一些证书吗(不确定)。

--已编辑--

import javax.net.ssl.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;

public class Test {
public static void disableCertificateValidation() {

// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {}
public void checkServerTrusted(X509Certificate[] certs, String authType) {}
}};

// Ignore differences between given hostname and certificate hostname
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) { return true; }
};

// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(hv);
} catch (Exception e) {}
}

public static void main(String[] args) throws Exception {
disableCertificateValidation();
try {
URL obj = new URL("https://xyz.abc.com");
HttpURLConnection connection = ((HttpURLConnection) obj.openConnection());
connection.addRequestProperty("User-Agent", "Mozilla/4.0");
System.out.println("--connection.getResponseCode() --" + connection.getResponseCode() ); //403
InputStream input;
if (connection.getResponseCode() == 200) // this must be called before 'getErrorStream()' works
input = connection.getInputStream();
else {
input = connection.getErrorStream();
System.out.println("in error stream : " + input);
}
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String msg;
while ((msg = reader.readLine()) != null)
System.out.println(msg);
} catch (IOException e) {
System.err.println(e);
}
}
}

输出:

--connection.getResponseCode() --403
in error stream : sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@685cb137
<html><head><meta http-equiv='refresh' content='1;url=/securityRealm/commenceLogin?from=%2Fview%2Ftools%2Fjob%2FMISC.DefectSummaryReport%2FlastSuccessfulBuild%2Fartifact'/><script>window.location.replace('/securityRealm/commenceLogin?from=%2Fview%2Ftools%2Fjob%2FMISC.DefectSummaryReport%2FlastSuccessfulBuild%2Fartifact');</script></head><body style='background-color:white; color:white;'>


Authentication required
<!--
You are authenticated as: anonymous
Groups that you are in:

Permission you need to have (but didn't): hudson.model.Hudson.Read
... which is implied by: hudson.security.Permission.GenericRead
... which is implied by: hudson.model.Hudson.Administer
-->

</body></html>

最佳答案

您可以尝试以下代码:

openConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, 如 Gecko) Chrome/23.0.1271.95 Safari/537.11");

关于java.io.IOException : Server returned HTTP response code: 403 for URL:unable to resolve,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44504205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com