gpt4 book ai didi

android - HttpsUrlConnection EOFException

转载 作者:行者123 更新时间:2023-11-30 03:06:35 27 4
gpt4 key购买 nike

我正在使用以下代码进行发布请求

public String executeHttpPost(String uri, String data) {

HttpURLConnection conn = null;
URL url = null;
String s = null;

try {

url = new URL(uri);

conn = (HttpURLConnection) url.openConnection();

if (conn instanceof HttpsURLConnection) {
Log.d("HTTPS", "HttpsUrl");
}

conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();

DisplayResponseHeaders(conn);

s = readStream(conn.getInputStream());
Log.d("body", s);

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
s = null;
e.printStackTrace();
} finally {
conn.disconnect();
}
return s;
}

当我通过 http 使用它时,一切正常,但通过 https 我得到

 java.io.EOFException
at libcore.io.Streams.readAsciiLine(Streams.java:203)
at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:573)
at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:821)
at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:283)
libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271)
ru.fors.remsmed.fragment.NetHelper.executeHttpPost(NetHelper.java:234)
ru.fors.remsmed.LoginActivity$UserLoginTask.doInBackground(LoginActivity.java:424)
ru.fors.remsmed.LoginActivity$UserLoginTask.doInBackground(LoginActivity.java:1)
android.os.AsyncTask$2.call(AsyncTask.java:287)
java.util.concurrent.FutureTask.run(FutureTask.java:234)
android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
java.lang.Thread.run(Thread.java:856)
threadid=14: thread exiting with uncaught exception (group=0x40a71930)

我在 httpsurlconnection 和可能的解决方案中发现了有关回收连接错误的问题:

if (Build.VERSION.SDK != null && Build.VERSION.SDK_INT > 13) {
conn.setRequestProperty("Connection", "close");
}

但这对我不起作用。

最佳答案

尝试像这样更改您的代码:

    conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded; charset: UTF-8");
byte[] output = data.getBytes("UTF-8");
conn.setFixedLengthStreamingMode(output.length);

os = conn.getOutputStream();
os.write(output);
os.flush();
os.close();

DisplayResponseHeaders(conn);

if (conn.getResponseCode() == 200) { // or other 2xx code like 204

s = readStream(conn.getInputStream());
Log.d("body", s);
}
else {

// handle error conditions like 404, 400, 500, ...

// now it may be necessary to read the error stream

InputStream errorStream = conn.getErrorStream();

// ...
}

据我所知,您应该始终关闭您打开的所有流。我不确定 conn.disconnect() 是否为您执行了该操作。

如果您想更方便地对 HTTP(S) 请求进行编码,可以查看 DavidWebb其中有一个库列表可帮助您避免使用繁琐的 HttpURLConnection。

关于android - HttpsUrlConnection EOFException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21723225/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com