Java HTTPClient 4.2.1仍然接收401 Unauthorized

时间:2012-08-20 16:01:26

标签: java authentication httpclient

尝试连接 SharePoint网址,所以我在httpcomponents-client-4.2.1 \ examples \ org \ apache \ http \中使用了示例代码examples \ client \ ClientAuthentication.java但将其更改为NTCredentials以忽略证书问题。 除此之外,代码是相同的,但输出:

executing requestGET url HTTP/1.1
----------------------------------------
HTTP/1.1 401 Unauthorized
Response content length: 0

这是完整的代码

public class DocumentApprover {

    static final String user = "user"; // your account name
    static final String pass = "password"; // your password for the account

    public static DefaultHttpClient wrapClient(DefaultHttpClient base) {
        try {
            SSLContext ctx = SSLContext.getInstance("TLS");
            X509TrustManager tm = new X509TrustManager() {

                public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
                }

                public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
                }

                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
            ctx.init(null, new TrustManager[]{tm}, null);
            SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            ClientConnectionManager ccm = base.getConnectionManager();
            SchemeRegistry sr = ccm.getSchemeRegistry();
            sr.register(new Scheme("https", 443, ssf));
            return new DefaultHttpClient(ccm, base.getParams());
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        try {

            DefaultHttpClient httpclient = wrapClient(new DefaultHttpClient());
            try {
                httpclient.getCredentialsProvider().setCredentials(
                    new AuthScope("host", 443),
                    new NTCredentials(user, pass, "workstation", "domain"));

                HttpGet httpget = new HttpGet("url");

                System.out.println("executing request" + httpget.getRequestLine());
                HttpResponse response = httpclient.execute(httpget);
                HttpEntity entity = response.getEntity();

                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                if (entity != null) {
                System.out.println("Response content length: " + entity.getContentLength());
                }
                EntityUtils.consume(entity);
            } finally {
                // When HttpClient instance is no longer needed,
                // shut down the connection manager to ensure
                // immediate deallocation of all system resources
                httpclient.getConnectionManager().shutdown();
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

为什么失败?如果凭据错误,我不会得到不同的错误吗?

0 个答案:

没有答案