我正在使用教程中的示例,但我想添加特殊标题,但如果我只是页面没有响应,我收到错误ERR_EMPTY_RESPONSE
。这是代码:
public class EchoHeaderHandler implements HttpHandler {
@Override
public void handle(HttpExchange httpExchange) throws IOException {
Headers headers = httpExchange.getRequestHeaders();
headers.add("Additional header", "3000");
System.out.println(headers.size());
Set<Map.Entry<String, List<String>>> entries = headers.entrySet();
String response = "";
for(Map.Entry<String, List<String>> entry : entries)
response += entry.toString() + "\n";
httpExchange.sendResponseHeaders(200, response.length());
OutputStream os = httpExchange.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
答案 0 :(得分:0)
您必须区分请求和响应。 您正在尝试添加到请求标头,这应该是不可能的...添加到响应标头 https://docs.oracle.com/javase/7/docs/api/javax/xml/ws/spi/http/HttpExchange.html#addResponseHeader(java.lang.String,%20java.lang.String)