java中的rest API的身份验证失败错误

时间:2015-11-04 17:52:07

标签: java rest authentication

我使用下面的代码来执行一些请求:

    DefaultHttpClient httpclient = new DefaultHttpClient();
    httpclient.getCredentialsProvider().setCredentials(
                new AuthScope("testrail.xyz.com", 80), 
                new UsernamePasswordCredentials("abc@xyz.com", "test123"));

    try {
      // specify the host, protocol, and port
      HttpHost target = new HttpHost("testrail.xyz.com", 80, "http");

      // specify the get request
      HttpGet getRequest = new HttpGet("/index.php?/api/v2/get_runs/52");
      getRequest.addHeader("Content-Type", "application/json;charset=UTF-8");

      System.out.println("executing request to " + target);

      HttpResponse httpResponse = httpclient.execute(target, getRequest);
      HttpEntity entity = httpResponse.getEntity();

      System.out.println("----------------------------------------");
      System.out.println(httpResponse.getStatusLine());
      Header[] headers = httpResponse.getAllHeaders();
      for (int i = 0; i < headers.length; i++) {
        System.out.println(headers[i]);
      }
      System.out.println("----------------------------------------");

      if (entity != null) {
        System.out.println(EntityUtils.toString(entity));
      }

我收到的回复是:

  

http://testrail.mypublisher.com:80

执行请求      

----------------------------------------

     

HTTP / 1.1 401 Unauthorized Date:Wed,04 Nov 2015 17:19:05 GMT Server:Apache X-Powered-By:   PHP / 5.3.3内容长度:87连接:关闭内容类型:   应用/ JSON;字符集= UTF-8

     

----------------------------------------

     

{&#34;错误&#34;:&#34;身份验证失败:用户/密码或会话Cookie无效或丢失。&#34;}

我传递凭据以进行身份​​验证,但仍然出现此错误。当我手动登录到应用程序或通过Firefox for Rest客户端触发上述请求时,它工作正常。 我哪里错了? 我使用的是4.5.1 httpclient jar文件。

1 个答案:

答案 0 :(得分:0)

试试这个(来自Apache HTTPClient文档):

/**
 * A simple example that uses HttpClient to execute an HTTP request against
 * a target site that requires user authentication.
 */
public class ClientAuthentication {

    public static void main(String[] args) throws Exception {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
                new AuthScope("httpbin.org", 80),
                new UsernamePasswordCredentials("user", "passwd"));
        CloseableHttpClient httpclient = HttpClients.custom()
                .setDefaultCredentialsProvider(credsProvider)
                .build();
        try {
            HttpGet httpget = new HttpGet("http://httpbin.org/basic-auth/user/passwd");

            System.out.println("Executing request " + httpget.getRequestLine());
            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                System.out.println(EntityUtils.toString(response.getEntity()));
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }
}