【问题标题】:HttpServer handler, additional Header cousing empty response in JavaHttpServer 处理程序,附加 Header 在 Java 中导致空响应
【发布时间】:2018-06-19 01:38:30
【问题描述】:

我正在使用教程中的示例,但我想添加特殊标题,但如果我只这样做,页面没有响应并且我收到错误 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();
    }
}

【问题讨论】:

    标签: java http http-headers httphandler


    【解决方案1】:

    您必须区分请求和响应。 您正在尝试添加到请求标头,这应该是不可能的...添加到响应标头 https://docs.oracle.com/javase/7/docs/api/javax/xml/ws/spi/http/HttpExchange.html#addResponseHeader(java.lang.String,%20java.lang.String)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-21
      • 2015-08-13
      • 2019-06-18
      • 2015-12-19
      • 2012-12-01
      • 2015-10-17
      • 2016-12-05
      • 1970-01-01
      相关资源
      最近更新 更多