2017-06-13 15 views
0

URLをヒットして、URLからコンテンツを読み込みたいとします。私が使ってそのURLへのアクセスを持ってHTTP/1.1 401無許可[サーバ:Apache-Coyote/1.1、キャッシュ制御:プライベート

response : HTTP/1.1 401 Unauthorized [Server: Apache-Coyote/1.1, Cache-Control: private, Expires: Wed, 31 Dec 1969 19:00:00 EST, X-Content-Type-Options: nosniff, WWW-Authenticate: Basic realm="Jenkins", Content-Type: text/html;charset=utf-8, Content-Language: en, Content-Length: 1072, Date: Tue, 13 Jun 2017 16:30:53 GMT] [email protected] 

: 私はこれを達成するために他の多くの方法をしようとしたがhere以下

import org.apache.http.*; 
import org.apache.http.auth.*; 
import org.apache.http.client.CredentialsProvider; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.client.protocol.ClientContext; 
import org.apache.http.impl.auth.BasicScheme; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.protocol.BasicHttpContext; 
import org.apache.http.protocol.ExecutionContext; 
import org.apache.http.protocol.HttpContext; 
import org.apache.http.util.EntityUtils; 

import java.io.IOException; 

/** 
* Simple class to launch a jenkins build on [email protected] platform, should also work on every jenkins instance (not tested) 
* 
*/ 
public class TestPreemptive { 
    public static void main(String[] args) { 

     // Credentials 
     String username = "username"; 
     String password = "password"; 

     // Jenkins url 
     String jenkinsUrl = "https://xyz.abc.com"; 

     // Build name 
     String jobName = "myReportname"; 

     // Build token 
     String buildToken = "successfulBuild"; 

     // Create your httpclient 
     DefaultHttpClient client = new DefaultHttpClient(); 

     // Then provide the right credentials 
     client.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), 
       new UsernamePasswordCredentials(username, password)); 

     // Generate BASIC scheme object and stick it to the execution context 
     BasicScheme basicAuth = new BasicScheme(); 
     BasicHttpContext context = new BasicHttpContext(); 
     context.setAttribute("preemptive-auth", basicAuth); 

     // Add as the first (because of the zero) request interceptor 
     // It will first intercept the request and preemptively initialize the authentication scheme if there is not 
     client.addRequestInterceptor(new PreemptiveAuth(), 0); 

     // You get request that will start the build 
     String getUrl = jenkinsUrl + "/job/" + jobName + "/build?token=" + buildToken; 
     HttpGet get = new HttpGet(getUrl); 

     try { 
      // Execute your request with the given context 
      HttpResponse response = client.execute(get, context); 
      System.out.print("response : " + response); 
      HttpEntity entity = response.getEntity(); 
      EntityUtils.consume(entity); 
     } 
     catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    static class PreemptiveAuth implements HttpRequestInterceptor { 

     public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { 
      // Get the AuthState 
      AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); 

      // If no auth scheme available yet, try to initialize it preemptively 
      if (authState.getAuthScheme() == null) { 
       AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth"); 
       CredentialsProvider credsProvider = (CredentialsProvider) context 
         .getAttribute(ClientContext.CREDS_PROVIDER); 
       HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); 
       if (authScheme != null) { 
        Credentials creds = credsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost 
          .getPort())); 
        if (creds == null) { 
         throw new HttpException("No credentials for preemptive authentication"); 
        } 
        authState.setAuthScheme(authScheme); 
        authState.setCredentials(creds); 
       } 
      } 

     } 

    } 
} 

から取られた以下のコードを参照してくださいsuccess.Pleaseものではありませんでしたが例外であります上記のエラーを解決するための提案は役に立ちます。 Authenticator APIを使用して資格情報を設定しようとしましたが、同じ問題に直面していました。

答えて

0

HTTP 401の不正なクライアントエラーステータス応答コードは、ターゲットリソースの有効な認証資格情報がないために要求が適用されなかったことを示します。私は認証オーセンティケータで問題があると思う。

関連する問題