1
votes

I am using below code to execute some request:

    DefaultHttpClient httpclient = new DefaultHttpClient();
    httpclient.getCredentialsProvider().setCredentials(
                new AuthScope("testrail.xyz.com", 80), 
                new UsernamePasswordCredentials("[email protected]", "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));
      }

The response that I receive is:

executing request to 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 Content-Length: 87 Connection: close Content-Type: application/json; charset=utf-8

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

{"error":"Authentication failed: invalid or missing user/password or session cookie."}

I passing the credentials for authentication and still I get this error. When I manually login to the application or fire the above request through a firefox Rest client, it works fine. Where am I going wrong? I am using 4.5.1 httpclient jar file.

1
is it basic Authentication you're trying to achieve?gidim
Yes basic authenticationjags_automation

1 Answers

0
votes

Try This (from Apache HTTPClient Documentation):

/**
 * 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();
        }
    }
}