【问题标题】:HttpURLConnection throws java.io.IOException: insufficient data writtenHttpURLConnection 抛出 java.io.IOException:写入的数据不足
【发布时间】:2016-03-30 21:35:56
【问题描述】:

我开发了一个调度程序应用程序,当我进行测试时,我得到了异常 IOException:写入的数据不足,这是引发错误的代码部分:

final int responseCode = connection.getResponseCode();

final HttpURLConnection connection = (HttpURLConnection) new URL(getUrl()).openConnection();
final int timeout =200;
final HttpRequestContext data = getData();//data contains the data send in the request to dispatche
connection.setConnectTimeout(timeout);
for (final Entry<String, List<String>> entry : data.getHeaders().entrySet()) {
    for (final String value : entry.getValue()) {
    connection.setRequestProperty(entry.getKey(), value);
    }
}
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
if (data.getContentLength() > 0) {
    connection.setFixedLengthStreamingMode(data.getContentLength());
}
connection.setReadTimeout(timeout);
connection.setRequestMethod(data.getMethod());
final int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
    final HttpRequestContext data = getData();

if (ArrayUtils.isEmpty(data.getBody())) {
    LOGGER.debug("No data to send");
} else {
    IOUtils.write(data.getBody(), connection.getOutputStream());
}
}

日志:

java.io.IOException: insufficient data written
at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.close(HttpURLConnection.java:3501)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1470)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at com.dhl.dispatcher.HttpRequestWriter.doRun(HttpRequestWriter.java:78)
at com.dhl.dispatcher.AbstractRequestWriter.run(AbstractRequestWriter.java:73)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

感谢您的帮助。

【问题讨论】:

  • 请发布完整的日志!
  • 为什么要将 content-length 设置为请求正文的长度?为什么你设置它但不发回任何东西?
  • 我更新了答案
  • 它可能会掩盖其他东西,因为它是“关闭时间”(例如:连接由于某种原因被服务器中止......

标签: java httpurlconnection


【解决方案1】:

你从不发送任何数据,但你设置了

connection.setFixedLengthStreamingMode(data.getContentLength());

在收到回复之前,您需要这样做:

        DataOutputStream out = new DataOutputStream(connection.getOutputStream());
        out.write(body);
        out.close();

更新: 在做

final int responseCode = connection.getResponseCode();

提出请求

您必须在此之前添加正文。

【讨论】:

  • 谢谢。我通过在 connection.getResponseCode(); 之前设置正文来解决问题;
  • 欢迎您!顺便说一句,如果您刚刚开始编写您的 android 客户端,我建议您尝试 github.com/apptik/jus 它基于 volley,但更加进化和更容易:)
【解决方案2】:

以下代码如果不使用可以删除:

if (data.getContentLength() > 0) {
    connection.setFixedLengthStreamingMode(data.getContentLength());
}

这是导致insufficient data written 异常的代码部分。

【讨论】:

    猜你喜欢
    • 2014-09-21
    • 2014-06-29
    • 1970-01-01
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    • 2014-06-28
    • 2012-09-01
    • 1970-01-01
    相关资源
    最近更新 更多