HTTPServer从服务器向客户端发送get请求

时间:2018-06-05 17:21:53

标签: java http

我想从http服务器向客户端发送参数(GET请求)。

    private void displayPage(HttpExchange he) throws IOException {
    String root = "html";
    URI uri = he.getRequestURI();
    File file = new File(root + uri.getPath()).getCanonicalFile();
    OutputStream os = null;
    String response = "";

    if (!file.isFile()) {
        // Object does not exist or is not a file: reject with 404 error.
        response = "404 (Not Found)\n";
        he.sendResponseHeaders(404, response.length());
        os = he.getResponseBody();
        os.write(response.getBytes());

    } else {
        // Object exists and is a file: accept with response code 200.
        he.sendResponseHeaders(200, 0);
        os = he.getResponseBody();
        FileInputStream fs = new FileInputStream(file);
        final byte[] buffer = new byte[0x10000];
        int count = 0;
        while ((count = fs.read(buffer)) >= 0) 
            os.write(buffer,0,count);  
        fs.close();
    }
    os.close();
}

此方法将请求.html页面发送给客户端。

我想在http标头中插入这些参数:

String urlParameters = "sensornumber="+N_SENSORI+"&lightnumber="+N_LUCI+"&defaultlightname="+LUCE+"&defaultsensornumber="+SENSORE;

所以我可以在.html文件中的javascript中使用它们

0 个答案:

没有答案