我正在使用HTTPClient
发出Web请求。从linux部分,使用以下命令将状态返回为OK
。来自java的同一件事返回未经授权的状态代码401
。从Linux无法发出HttpClient请求
命令:
curl -X GET -ik -H 'Accept: application/json' --user test:test http://api.uat.testapi.com
要访问此,我使用如下java代码:
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());
我得到的回应是:HTTP/1.1 401 Unauthorized
。
任何解决此问题的指针都会有帮助
在此先感谢。
在命令中,您似乎使用'GET',而在使用'POST'的Java代码中 - 从我的POV中比较两个相当不同的东西... – Yahia
与GET方法一样的事情发生了。 – user972590
我尝试访问的原始主机受到用户名/密码的保护,因此,使用httpClient的setCredentials不会将凭据绑定到requestURI。我认为。 – user972590