尝试使用httpclient将授权凭据发送到archiva

时间:2015-02-25 21:22:52

标签: apache-httpclient-4.x archiva

我试图ping我的archiva服务器,这对于不需要授权的网页来说不是问题。但是,当我尝试pingWithAutz时,我忍不住得到403错误。以下是相关的代码段:

         public FormValidation doTestConnection(@QueryParameter("url") String url,
             @QueryParameter("username") String usr,
             @QueryParameter("password") String pwd)
             throws Exception {

         if (url == null || url.equals("")) {
             return FormValidation.error("Please, set a correct url");
         }
         url = url.endsWith("/") ? url.substring(0, url.length() - 1) : url;
         HttpPost post = new HttpPost(url + "/restServices/redbackServices/loginService/pingWithAutz");
         String authzHeader = "Basic " + Base64Utility.encode(( usr + ":" + pwd ).getBytes());
         post.setHeader("Authorization",authzHeader);

         HttpClientBuilder builder = HttpClientBuilder.create();
         CloseableHttpClient httpclient = builder.build();
         CloseableHttpResponse response = null;

         try {
             response = httpclient.execute(post);

             if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
                 return FormValidation.error("Cannot connect to the server. Response Code : " +
                         response.getStatusLine());
             }
         } catch(Exception e){
             System.out.println(e);
         }
         finally {
             response.close();
         }           
         return FormValidation.ok("Successfully connected to Archiva Server.");
     }

我做错了什么,它不能正确认证?

1 个答案:

答案 0 :(得分:0)

您应该尝试使用HttpGet,因为此服务需要GET http而不是POST。 见http://archiva.apache.org/docs/2.2.0/rest-docs-redback-rest-api/resource_LoginService.html#path__loginService_ping.html

HTH

相关问题