Android手机中服务器HTTP响应的状态码打印在响应正文中

问题描述 投票:0回答:2

我正在使用 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 响应。我没有在我的代码中设置这个。如有任何帮助,我们将不胜感激。

enter image description here

android httpresponse
2个回答
0
投票

这可能对你有帮助... 我认为

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();
        }

0
投票

我定义的handle()方法有问题。我为每个请求创建一个新的 HttpResponse,而不是使用

中传递的响应
public void handle(HttpRequest request, HttpResponse response, HttpContext context)
© www.soinside.com 2019 - 2024. All rights reserved.