2012-01-08 71 views
2

我能夠使用HttpClient成功發佈到servlet。然而,我需要一個布爾參數從servlet返回到HttpClient,我不知道如何做到這一點。如何從HTTPClient調用的Servlet獲取對HTTPClient的響應?

Earleir我使用的是common-httpclient-3.1.jar.However然後我意識到httpclient的版本升級到4.0,它協助從HttpResponse響應處理。現在我正在使用httpclient.4.0.3,httpcore-4.1和httpmime-4.1.1。我正在嘗試的代碼如下面的HttpClient。

HttpClient client = new DefaultHttpClient(); 
HttpPost method = new HttpPost("http://localhost:9090/drools-guvnor/KeystoneServlet"); 
HttpEntity entity = method.getEntity(); 
List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>(); 
nvps.add(new BasicNameValuePair("directory", directory)); 
nvps.add(new BasicNameValuePair("username", username)); 
nvps.add(new BasicNameValuePair("password", password)); 
method.setEntity(new UrlEncodedFormEntity(nvps)); 
ResponseHandler responseHandler = new BasicResponseHandler(); 
HttpResponse response = client.execute(method, responseHandler); 
System.out.println("responseBody: "+responseHandler.handleResponse(response)); 

在KeyStoneServlet中,我能夠成功獲取參數目錄,用戶名和密碼。進一步使用這些參數做進一步處理,並能夠檢索一個布爾參數,我想要回作爲對HttpClient的響應。但是我不知道我該怎麼辦。上面的responseBody SOP變成了空白,並且沒有顯示任何東西。

任何在同樣的援助將不勝感激。

+0

你是什麼意思「上面的responseBody SOP變爲空白」?什麼是servlet代碼的樣子? – 2012-01-08 08:02:00

+0

boolean result = localAuth.Login(directory,new Object [] {username,password}); System.out.println(「Auth的結果: - 」+ result);我想返回這個布爾參數回到HttpClient – user1136819 2012-01-08 08:29:16

回答

0

有許多方法可以從servlet返回數據。JSON,XML,純文本。你所要做的就是:

response.setContentType("text/xml"); 
PrintWriter pw=response.getWriter(); 
pw.write(<reponse data string xml>); 

接收httpclient上的數據並解析它。如果你需要的只是布爾值,那麼一個表示真/假的字符串應該可以。但是如果你想從你的servlet拋出一個異常呢?在這種情況下,XML更健壯。

如果不想解析XML或文本和處理響應,然後嘗試使用Web服務..

編輯:在迴應評論:

在你的servlet,您可以訪問一個HTTPResponse對象。我們使用響應來表示這個對象。

response.setContentType("text/plain"); 
PrintWriter pw=response.getWriter(); 
pw.write(<true or false based on your processing>); 
在客戶端代碼

現在,我還沒有太多的工作與HttpClient的4 ..所以這是HttpClient的3 -

HttpClient client = new DefaultHttpClient(); 
HttpPost method = new HttpPost("http://localhost:9090/drools-guvnor/KeystoneServlet"); 
HttpEntity entity = method.getEntity(); 
List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>(); 
nvps.add(new BasicNameValuePair("directory", directory)); 
nvps.add(new BasicNameValuePair("username", username)); 
nvps.add(new BasicNameValuePair("password", password)); 
method.setEntity(new UrlEncodedFormEntity(nvps)); 
HttpResponse httpResponse = client.execute(method); 
int statusCode = httpResponse.getStatusLine().getStatusCode(); 
if (statusCode == HttpStatus.SC_OK) { 
    InputStream is = httpResponse.getEntity().getContent(); 

    //convert your stream into a string and convert it into a boolean 
} 

對不起我沒有HttpClient的4對證。但我確保以上應該工作..

我不會建議發回純文本。而是使用XML並按上述相同的方式處理它。

+0

Sethu,我將不勝感激,如果你能告訴我我將如何返回我使用的布爾參數,如下面在servlet中所示返回到 HttpClient布爾結果= localAuth.Login (目錄,新的對象[] {用戶名,密碼}); System.out.println(「Auth的結果: - 」+ result); HttpResponse給我空的迴應,因此我無法理解。大聲呼籲我將屬性設置爲HttpServletResponse ........雖然在那也只能設置字符串,而不是布爾值。 – user1136819 2012-01-08 09:43:30

+0

我已經通過回覆編輯回覆... – sethu 2012-01-08 11:00:19

+0

謝謝你回答Sethu。你在回覆中提到了HttpPost,而你正在使用httpclient3。我相信我應該使用PostMethod ......如果我錯了,請糾正我。 httpclient3中也不存在設置實體,因此您提供的代碼片段幾乎不能工作。 :( 另外我仍然擔心如何從servlet獲取布爾參數回到HttpClient。 – user1136819 2012-01-08 11:44:03