我正在使用 http://hc.apache.org/httpcomponents-core-ga/httpcore/examples/org/apache/http/examples/ElementalHttpServer.java 适用于 Android。
我使用以下代码设置响应:
HttpResponse getResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, 404, "Not Found");
getResponse.setEntity(new StringEntity(new String("The requested resource " + target + " could not be found due to mismatch!!")));
conn.sendResponseHeader(getResponse);
conn.sendResponseEntity(getResponse);
我在 Mozilla Poster 或浏览器中的响应的标头为 404,响应正文为:
The requested resource could not be found due to mismatch!!HTTP/1.1 200 OK
如何仅获取 HTTP 正文字符串?为什么我收到 HTTP/1.1 200 OK 响应。我没有在我的代码中设置这个。如有任何帮助,我们将不胜感激。
这可能对你有帮助... 我认为
response
就是你所需要的
HttpClient client = new DefaultHttpClient();
HttpResponse httpResponse;
try {
httpResponse = client.execute(request);
responseCode = httpResponse.getStatusLine().getStatusCode();
message = httpResponse.getStatusLine().getReasonPhrase();
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
response = convertStreamToString(instream);
// Closing the input stream will trigger connection release
instream.close();
}
} catch (ClientProtocolException e) {
client.getConnectionManager().shutdown();
e.printStackTrace();
} catch (IOException e) {
client.getConnectionManager().shutdown();
e.printStackTrace();
}
我定义的handle()方法有问题。我为每个请求创建一个新的 HttpResponse,而不是使用
中传递的响应public void handle(HttpRequest request, HttpResponse response, HttpContext context)