Android Https状态码-1

Android Https状态码-1,第1张

概述我正在通过设置从androids应用程序通过HttpsUrlConnection或HttpUrlConnection连接到Web服务器.目前,我没有任何问题,但是昨天我开始收到http/https状态响应-1.即使有某种错误,Web服务器也无法将其返回给我.我连接到的服务器旨在在出现某种问题时返回errorCode和errorString.这是

我正在通过设置从androids应用程序通过httpsUrlConnection或httpUrlConnection连接到Web服务器.目前,我没有任何问题,但是昨天我开始收到http / https状态响应-1.即使有某种错误,Web服务器也无法将其返回给我.我连接到的服务器旨在在出现某种问题时返回errorCode和errorString.这是我正在使用的代码,但我不认为问题出在这里.

    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应该代表什么.它是对某种错误还是其他的响应?

解决方法:

http响应代码-1,表示连接或响应处理出错. httpURLConnection经常使连接保持活动状态.

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

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

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

以上是内存溢出为你收集整理的Android Https状态码-1全部内容,希望文章能够帮你解决Android Https状态码-1所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1084383.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存