Java

时间:2018-11-08 21:26:43

标签: java http server request put

我目前正在学习如何在Java中使用HTTP请求,我已经完成了一个可以正常工作的GET请求:

static class ThermostatHandler implements HttpHandler {

    @Override
    public void handle(HttpExchange he) throws IOException {
        String requestMethod = he.getRequestMethod();
        if (requestMethod.equalsIgnoreCase("GET")) {
            String response = Smart.thermostatsGlobal;
            addResponseHeader(he);
            he.sendResponseHeaders(200, response.length());
            try (OutputStream os = he.getResponseBody()) {
                os.write(response.getBytes());
            } 
        }             
    }
}

现在我正在尝试执行PUT请求,但使用这些导入找不到任何帮助:

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

我不能使用其他导入,我可以使用 .httpserver添加一些内容。

任何有关如何发送数据的帮助都可以告诉Smart.thermostatsGlobal,但使用PUT可以是任何类型的数据,但我更喜欢字符串

路由为localhost:8000 / thermostats

谢谢

编辑:

出于学习目的,我需要在Java 8上不幸地使用这些sun包和im

0 个答案:

没有答案