HttpUrlConnection Expect-100管道中断

时间:2018-07-20 16:35:11

标签: java android http httpurlconnection

我在http标头中使用了Expect 100-continue,如下面的代码所示。当我得到outputstream时,HttpUrlConnection不要等待100 Expectation或任何其他400错误的答案。 (在RFC中,他们说这不是问题:https://tools.ietf.org/html/rfc7231#section-5.1.1)。

我已经看过这篇文章:How to wait for Expect 100-continue response in Java using HttpURLConnection,以防他们有更多关于该问题的信息,但是我已经测试了setChuncked方法,只是看它是否有所作为,而没有。

问题是,当服务器回答错误并关闭连接却没有让我完成上传时,我遇到了管道中断的问题。是的,我在一个紧密的连接上写字节,但是我不能正确处理。我看不到服务器的答案。我不知道连接在写之前是关闭的。如果我尝试获取ErrorStream,则会得到与InputStream,ResponseCode和其他方法相同的null引用,以了解发生了什么。

有什么主意如何使它正确,或者我想让它正常工作吗?

javax.net.ssl.SSLException: Write error: ssl=0x73de8c02c0: I/O error during system call, Broken pipe
at com.android.org.conscrypt.NativeCrypto.SSL_write(Native Method)
at com.android.org.conscrypt.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:824)
at com.android.okhttp.okio.Okio$1.write(Okio.java:76)
at com.android.okhttp.okio.AsyncTimeout$1.write(AsyncTimeout.java:155)
at com.android.okhttp.okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:176)
at com.android.okhttp.okio.RealBufferedSink.write(RealBufferedSink.java:46)
at com.android.okhttp.internal.http.HttpConnection$ChunkedSink.write(HttpConnection.java:339)
at com.android.okhttp.okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:176)
at com.android.okhttp.okio.RealBufferedSink$1.write(RealBufferedSink.java:198)
at out.write(buffer, 0, bytes_read);

在下面的代码中,我删除了一些不有用的部分,例如对此进行了全局尝试,或为fileSize计算。

huc = (HttpURLConnection) new URL(upload_url).openConnection();
huc.setRequestMethod("POST");
huc.setRequestProperty("Content-Length", "" + FILE_SZ);
huc.setRequestProperty("User-Agent", "UA");
huc.setRequestProperty("Expect","100-continue");


huc.setFixedLengthStreamingMode(FILE_SZ);
huc.setUseCaches(false);
huc.setDoInput(true);
huc.setDoOutput(true);
huc.setInstanceFollowRedirects(true);
try {
   // read input file chunks + publish progress
   OutputStream out = new OutputStream(huc.getOutputStream());
   buffer_size = 4 * 1024;
   buffer = new byte[buffer_size];
   total_bytes = 0;
   while ((bytes_read = fis.read(buffer)) != -1) {
        out.write(buffer, 0, bytes_read);
        total_bytes += bytes_read;
   }
   out.flush();
   out.close();
} catch (IOException e) {
    e.printStackTrace();
}

0 个答案:

没有答案