4

SharePoint URLに接続しようとしているので、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();
        }

    }

}

なぜ失敗するのですか?資格情報が間違っていた場合、別のエラーが発生しませんか?

4

0 に答える 0