java.io.IOException:服务器返回HTTP响应代码:403 for URL:无法解析

时间:2017-06-12 16:19:54

标签: 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>  

1 个答案:

答案 0 :(得分:1)

你可以尝试下面的代码:

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