我使用下面的代碼來執行一些要求:驗證失敗的錯誤休息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文件。
是你想實現基本的身份驗證? – gidim
是基本身份驗證 –