2015-11-04 51 views
1

我使用下面的代碼來執行一些要求:驗證失敗的錯誤休息API在Java中

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)); 
     } 

,我收到的迴應:

執行請求http://testrail.mypublisher.com:80

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

HTTP/1.1 401未授權日期: Wed,04 Nov 2015 17:19:05 G MT服務器:Apache X供電-通過: PHP/5.3.3內容長度:87連接:關閉內容類型: 應用/ JSON;字符集= UTF-8

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

{「錯誤」:「驗證失敗:無效或丟失的用戶/密碼或會話cookie」}

我傳遞憑據進行身份驗證,我仍然得到這個錯誤。當我手動登錄到應用程序或燒透Firefox的REST客戶端的上述要求,它工作正常。 我哪裏錯了? 我使用4.5.1 HttpClient的jar文件。

+0

是你想實現基本的身份驗證? – gidim

+0

是基本身份驗證 –

回答

0

試試這個(Apache的文檔的HTTPClient):

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

嗨Gidim,我得到這樣的回覆.... –

+0

HTTP/1.1 200 OK <!DOCTYPE html PUBLIC「 - // W3C // DTD XHTML 1.0 Strict // EN」「http:// www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd「> \t \t 在URI \t 無效字符:[/ API/V2/get_runs/52 /基本-AUTH/anadig @ xyz_com/test123] - TestRail \t \t \t <鏈接類型= 「文本/ CSS」 的rel = 「樣式表的」 href = 「CSS /差錯combined.css?3294」 \t \t \t媒體= 「所有」/> \t <鏈路的rel =」快捷方式圖標「href =」images/favicon.ico「/> –

+0

看起來像一個編碼問題。嘗試使用不帶@的用戶名。如果這樣的作品,那麼你需要Base64編碼,你的頭。 – gidim