I am using HTTPClient to make a web request. From the linux part, using the following command returns the status as OK. The same thing from the java is returning unauthorized status code 401.
Command from linux:
curl -X GET -ik -H 'Accept: application/json' --user test:test http://api.uat.testapi.com
To access this, I am using java code as below:
HttpHost targetHost = new HttpHost("http://api.uat.testapi.com/");
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials( new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials("test","test"));
HttpGet httppost = new HttpGet("http://api.uat.testapi.com/");
httppost.addHeader("Accept","application/json");
HttpResponse response = httpclient.execute(httppost);
System.out.println("status>>>>"+response.getStatusLine());
I am getting the response as: HTTP/1.1 401 Unauthorized.
Any pointers to solve this will be helpful
Thanks in advance.
I think you need to set HttpState to use authentication. Use
to do the same.