2011-11-24 111 views
0

我正在使用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

任何解决此问题的指针都会有帮助

在此先感谢。

+1

在命令中,您似乎使用'GET',而在使用'POST'的Java代码中 - 从我的POV中比较两个相当不同的东西... – Yahia

+0

与GET方法一样的事情发生了。 – user972590

+0

我尝试访问的原始主机受到用户名/密码的保护,因此,使用httpClient的setCredentials不会将凭据绑定到requestURI。我认为。 – user972590

回答

0

我认为你需要设置HttpState来使用身份验证。使用

httpClient.getParams().setAuthenticationPreemptive(true); 

也这样做。

+0

嗨,我使用http 4.1 apache客户端,我没有找到mehtod setAuthenticationPReemptive(true),那里?请告诉我如何设置。 – user972590

+0

好的。对于Http 4.1,您可以使用... HttpContext localContext = new BasicHttpContext(); local.text.setAttribute(“http.auth.scheme-pref”,Arrays.asList(new String [] {“basic”})); 012h 我认为这将起作用。:-) –

+0

嗨Abhutra,我真的很陌生,我已经粘贴了代码,设置凭据后,仍然得到相同的响应。我需要做什么其他更改? – user972590

0

看起来你正在用curl和POST请求使用HttpClient进行GET请求;我怀疑POST请求没有被服务器接受。

你需要使用HTTPGET代替:

HttpGet method = new HttpGet("http://api.uat.testapi.com"); 
... 
HttpResponse response = httpclient.execute(method); 

希望帮助...

0
try below code : 

HttpURLConnection connection = (HttpURLConnection) new URL("url").openConnection(); 

connection.setDoOutput(true); 
connection.setDoInput(true); 
connection.setRequestMethod("GET"); 
connection.setRequestProperty("Accept", "application/json"); 
connection.setRequestProperty("Authorization", "Basic "+ 
      new sun.misc.BASE64Encoder().encode("username:password".getBytes())); 

System.out.println("status>>>>"+connection.getResponseCode()); 
0

尝试使用follwing代码....根据UR设置前URL等进行修改

DefaultHttpClient httpclient = new DefaultHttpClient(); 
    try { 
     httpclient.getCredentialsProvider().setCredentials(
       new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), 
       new UsernamePasswordCredentials("username", "password")); 

     HttpGet httpget = new HttpGet("YOURURL"); 

     System.out.println("executing request" + httpget.getRequestLine()); 
     HttpResponse response = httpclient.execute(httpget); 
     HttpEntity entity = response.getEntity(); 

     System.out.println("----------------------------------------"); 
     System.out.println(response.getStatusLine()); 
     if (entity != null) { 
      System.out.println("Response content length: " + entity.getContentLength()); 
     } 
     EntityUtils.consume(entity); 
    } 
catch(Exception e) 
{ 
e.printStackTrace(); 
} 
finally { 
     // When HttpClient instance is no longer needed, 
     // shut down the connection manager to ensure 
     // immediate deallocation of all system resources 
     httpclient.getConnectionManager().shutdown(); 
    }