Java 如何在Android中连接到服务器时获取错误消息

Java 如何在Android中连接到服务器时获取错误消息,java,android,http-status-code-403,Java,Android,Http Status Code 403,我使用GET方法连接到服务器,服务器响应http状态代码403。 当我将GET方法的url粘贴到浏览器时,收到一些文本和http状态码403。但当我通过JavaAndroid的HttpURLConnection向服务器发送一个具有相同url的GET请求时,我只收到http状态码403,响应文本为空。 所以,当服务器返回代码403时,任何人都可以告诉我如何获取一些文本。 提前谢谢 尝试使用DefaultHttpClient类 client = new DefaultHttpClient(httpP

我使用GET方法连接到服务器,服务器响应http状态代码403。 当我将GET方法的url粘贴到浏览器时,收到一些文本和http状态码403。但当我通过JavaAndroid的HttpURLConnection向服务器发送一个具有相同url的GET请求时,我只收到http状态码403,响应文本为空。 所以,当服务器返回代码403时,任何人都可以告诉我如何获取一些文本。
提前谢谢

尝试使用DefaultHttpClient类

client = new DefaultHttpClient(httpParameters);

                httpResponse = client.execute(request);
                responseCode = httpResponse.getStatusLine().getStatusCode();

                HttpEntity entity = httpResponse.getEntity();

                if (entity != null) {

                    InputStream instream = entity.getContent();
                    response = convertStreamToString(instream);
                    instream.close();
                }

只需按照Zoombie编写的操作并添加行:

String reasonPhrase = httpResponse.getStatusLine().getReasonPhrase();
如果它不工作,则服务器不会设置原因。然后,您应该将代码映射到标准原因短语:


好的,我在这里找到了答案:谢谢你的帮助!