Android Https状态码-1 [英] Android Https Status code -1

查看:165
本文介绍了Android Https状态码-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要根据设置从Android应用程序通过HttpsUrlConnectionHttpUrlConnection连接到Web服务器.目前,我没有任何问题,但是昨天我开始得到http/https status响应-1.即使有某种错误,Web服务器也无法将其返回给我.我连接到的服务器被设计为在出现某种问题时返回errorCodeerrorString.这是我正在使用的代码,但我不认为问题出在这里.

I'm connecting to a web server from my android application via HttpsUrlConnection or HttpUrlConnection depending on settings. For now I didn't have any kind of problem, but yesterday I start getting http/https status response -1. There is no way the web server return me this even if there is some kind of error. The server which I'm connection to is designed to return errorCode and errorString when there is some kind of problem. Here is the code I'm using,but I don't think the problem is here.

    public void UseHttpConnection(String url, String charset, String query) {
    try {
        HttpURLConnection connection = (HttpURLConnection) new URL(url)
                .openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Charset", charset);
        connection.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded;charset=" + charset);
        OutputStream output = null;
        try {
            output = connection.getOutputStream();
            output.write(query.getBytes(charset));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (output != null)
                try {
                    output.close();
                } catch (IOException logOrIgnore) {
                }
        }

        int status = ((HttpURLConnection) connection).getResponseCode();
        Log.i("", "Status : " + status);

        for (Entry<String, List<String>> header : connection
                .getHeaderFields().entrySet()) {
            Log.i("Headers",
                    "Headers : " + header.getKey() + "="
                            + header.getValue());
        }

        InputStream response = new BufferedInputStream(
                connection.getInputStream());

        int bytesRead = -1;
        byte[] buffer = new byte[30 * 1024];
        while ((bytesRead = response.read(buffer)) > 0) {
            byte[] buffer2 = new byte[bytesRead];
            System.arraycopy(buffer, 0, buffer2, 0, bytesRead);
            handleDataFromSync(buffer2);
        }
        connection.disconnect();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

所以我的问题是,-1应该代表什么.是对某种错误还是其他的响应?

So my question is, what should -1 stands for. Is it response for some kind of error or something else?

推荐答案

HTTP响应代码-1,表示连接或响应处理出现问题. HttpURLConnection经常使连接保持活动状态.

HTTP response code -1, means that something went wrong with connection or response handling. The HttpURLConnection in often buggy with keeping connections alive.

如果要关闭它,则必须将http.keepAlive系统属性设置为false.

If you want to turn if off, you have to set the http.keepAlive system property into false.

以编程方式执行此操作的方法是将其放在应用程序的开头:

The way to do this programmatically is putting this at the beginning of your application:

System.setProperty("http.keepAlive", "false");

这篇关于Android Https状态码-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆