gpt4 book ai didi

java - HttpURLConnection 抛出 java.io.IOException : insufficient data written

转载 作者:搜寻专家 更新时间:2023-11-01 03:00:25 25 4
gpt4 key购买 nike

我开发了一个调度程序应用程序,当我进行测试时,我得到了异常 IOException: insufficient data written 这是抛出错误的代码部分:

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)

感谢您的帮助。

最佳答案

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

connection.setFixedLengthStreamingMode(data.getContentLength());

在你得到回应之前你需要像这样做:

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

更新:做

final int responseCode = connection.getResponseCode();

发出请求

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

关于java - HttpURLConnection 抛出 java.io.IOException : insufficient data written,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36320179/

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