的authenticate_user方法旨在創建它通過REST API來獲取在Java中的項目清單:獲取JSON響應,而不是text/html的
ClientConfiguration類:
public HttpResponse clientConfig(String username, String password, String prms)
{
try
{
HttpClient httpclient = HttpClients.createDefault();
HttpClientContext httpContext = HttpClientContext.create();
HttpGet httpget = new HttpGet("http://" + _HOST + "/" + prms);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("Type", "Basic Auth"));
nvps.add(new BasicNameValuePair("Username", username));
nvps.add(new BasicNameValuePair("Password", password));
httpget.setHeader("Authorization", "Basic " + new UrlEncodedFormEntity(nvps));
// Execute and get the response.
httpget.setHeader(HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8");
HttpResponse response = httpclient.execute(httpget, httpContext);
response.setHeader("Content-Type", "application/json");
if (response.getStatusLine().getStatusCode() != 200)
{
throw new IOException("Non-successful HTTP response: " + response.getStatusLine().getStatusCode() + ":"
+ response.getStatusLine().getReasonPhrase());
}
else if (response.getStatusLine().getStatusCode() == 200)
{
System.out.println("Response>>>" + response);
System.out.println("HTTPResponse: " + response.getStatusLine().getStatusCode() + ":"
+ response.getStatusLine().getReasonPhrase());
flag = true;
return response;
}
else
{
System.out.println("Status is not 200");
}
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
而我的輸出是html響應而不是html響應我需要JSON格式,它返回我登錄頁面
HTTPResponse: 200:OK
<!DOCTYPE html>
<html lang="en">
................ </html>
ContentMimeType: text/html
SuccessFully login
任何人都可以幫助我找出問題所在。
你是否在服務器端使用Jersey(它看起來是這樣)?如果是這樣,那麼你需要圍繞你的返回布爾值創建一個包裝器(即一個包含/包裝這個布爾值的簡單類) –
ok,但是我使用了HTTPRequest和Response。 –
是的,我可以看到您使用的是Apache HTTP客戶端,但我詢問了有關服務器框架。 –