简体   繁体   English

如何从HTTPClient调用的Servlet获取对HTTPClient的响应?

[英]How to get a response to HTTPClient from a Servlet which is called from the HTTPClient?

I am able to successfully post to a servlet using HttpClient. 我能够使用HttpClient成功发布到servlet。 However i need a boolean parameter back to the HttpClient from the servlet and am not sure how to do the same. 但是我需要一个从servlet返回到HttpClient的布尔参数,并且不确定如何执行相同的操作。

Earleir i was using the common-httpclient-3.1.jar.However then i was made aware of the version upgrade of httpclient to 4.0 which assists in the Response handling from HttpResponse. Earleir我当时使用的是common-httpclient-3.1.jar。但是后来我意识到将httpclient升级到4.0,这有助于HttpResponse处理响应。 Now I am using httpclient.4.0.3, httpcore-4.1 and httpmime-4.1.1. 现在我正在使用httpclient.4.0.3,httpcore-4.1和httpmime-4.1.1。 The code being tried by me is as below for the HttpClient. 我正在尝试的代码如下所示,用于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));

In the KeyStoneServlet i am able to successfully get the parameters directory, username and password. 在KeyStoneServlet中,我能够成功获取参数目录,用户名和密码。 Further using these parameter am doing a further processing and am able to retrieve a boolean parameter which i want back as a response to the HttpClient. 进一步使用这些参数可以进行进一步的处理,并且能够检索一个布尔参数,我想将其作为对HttpClient的响应。 However i do not know how can i do the same. 但是我不知道我该怎么做。 The responseBody SOP above comes blank and doesnt show me anything. 上面的responseBody SOP空白,没有显示任何内容。

Any assistance on the same will be highly appreciated. 对此的任何协助将不胜感激。

There are many ways to return data back from the servlet.. JSON, XML, plain text. 有很多方法可以从servlet返回数据。JSON,XML,纯文本。 All you have to do is: 您要做的就是:

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

Receive the data on the httpclient and parse it. 在httpclient上接收数据并进行解析。 If all you need is the boolean then a single string saying true/false should do.. But what if there is an exception you want to throw from your servlet? 如果您只需要布尔值,那么应该使用单个字符串true / false进行操作。但是,如果要从Servlet中抛出异常,该怎么办? In that case an XML is more robust. 在这种情况下,XML更加健壮。

If dont want to parse xml or text and handle the response, then try using web services.. 如果不想解析xml或文本并处理响应,请尝试使用Web服务。

EDIT: In response to the comment: 编辑:回应评论:

In your servlet, you have access to a HTTPResponse object. 在您的Servlet中,您可以访问HTTPResponse对象。 Let's use response to represent this object. 让我们使用响应来表示该对象。

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

Now in your client code, I havent worked much with httpclient 4.. so this is with httpclient 3 - 现在在您的客户端代码中,我与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
}

Sorry I didnt have httpclient 4 to check against.. But I am sure the above should work.. 抱歉,我没有httpclient 4可以检查。。但是我确信上面的方法可以工作..

I wouldn't recommend sending plain text back. 我不建议发送回纯文本。 Instead work with XML and process it the same way as above. 而是使用XML并以与上述相同的方式处理它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM