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.