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

标签 java

我面临以下异常:

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/

相关文章:

Java(Jsoup) : How to parse http://host:port

Java、套接字连接和 HTTP 请求/响应

java - 什么是非常大的 HashMap 的有效替代方案?

java - Spring 4 + Tiles (3 || 2.2.2) - 有什么方法可以访问 View 上下文中的 bean?

java - 格式化值时如何避免 null

Java BlockingQueue<Runnable> 不可转换类型

java - 是否可以从 xml 配置向 Hazelcast 分布式映射添加值?

java - JBoss AS 5.x 停产了吗?

java - 我可以将 Google Play 服务添加到已发布的应用程序中吗?

java - WildFly 9 中新的缺失/不满足的依赖项错误