- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在经历this问题虽然不确定是否准确。
我发现日志中读取输入后无法写入输出
,并且根据上述内容,我相信发生这种情况是因为 getResponseCode()
后跟 >getOutputStream()
.
这是否是我看到的记录错误的原因?
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if(conn.getResponseCode() == 0){
logger.debug("Success");
} else {
logger.debug("Time out set for 30 seconds");
}
String input = writer.getBuffer().toString();
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
最佳答案
以下代码
StringWriter writer = new StringWriter();
String pushURL = "Your URL";
URL url = new URL(pushURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/restService");
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
if(conn.getResponseCode() == 0){
logger.debug("Success");
} else {
logger.debug("Time out set for 30 seconds");
}
String input = writer.getBuffer().toString();
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
由于以下位置 here 将导致 java.net.ProtocolException
:
The HTTP protocol is based on a request-response pattern: you send your request first and the server responds. Once the server responded, you can't send any more content, it wouldn't make sense. (How could the server give you a response code before it knows what is it you're trying to send?)
So when you call server.getResponseCode(), you effectively tell the server that your request has finished and it can process it. If you want to send more data, you have to start a new request.
因此,OP 在 conn.getResponseCode()
之后调用 conn.getOutputStream();
,生成 java.net.ProtocolException
> 其 Exception.getMessages()
产生
Cannot write output after reading input
关于java - 读取输入后无法写入输出;经历过这种情况但不确定原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40852262/
我正在使用 libapr,但它们的一些基本原语似乎不能很好地工作,呈现出非常奇怪的行为。这是我正在编写的代码: pr_pool_t *mp=NULL; apr_file_t *fp =
我多年来一直在寻找这个问题的答案,但在任何地方都找不到。真心希望有人能帮忙 我的结构: WebContent - resources - css - style.css
功能: 我正在尝试创建一个有 5 次尝试的测验,其中每个问题页面都是从每个类别中显示的随机问题。 因此,如果用户答对了每个问题,下一个问题将淡入,如果用户在 5 次尝试中有 3 次答对了 3 个问题,
我是一名优秀的程序员,十分优秀!