从HttpExchange获取POST数据

时间:2017-05-06 15:35:53

标签: java http httpserver http-post-vars

这似乎是一个常见的用例,应该有一个简单的解决方案,但我看到的每个地方都充斥着极其臃肿的例子。

我们说我的表格看起来像是:

<form action="http://localhost/endpoint" method="POST" enctype="multipart/form-data">
    <input type="text" name="value" />
    <input type="submit" value="Submit"/>
</form>

我提交它并在服务器上想要获取输入值:

在服务器上,我设置了一个带有单个端点的HttpServer,在这种情况下,只是直接发送正文数据:

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

import java.io.*;
import java.net.InetSocketAddress;
import java.util.function.Consumer;

public class Main {
    public static void main(String[] args) {
        try {
            HttpServer server = HttpServer.create(new InetSocketAddress(80), 10);
            server.setExecutor(null);
            server.createContext(
                    "/endpoint",
                    (HttpExchange httpExchange) -> {

                            InputStream input = httpExchange.getRequestBody();
                            StringBuilder stringBuilder = new StringBuilder();

                            new BufferedReader(new InputStreamReader(input))
                                .lines()
                                .forEach( (String s) -> stringBuilder.append(s + "\n") );

                            httpExchange.sendResponseHeaders(200, stringBuilder.length());

                            OutputStream output = httpExchange.getResponseBody();
                            output.write(stringBuilder.toString().getBytes());

                            httpExchange.close();
                    }
            );

            server.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

现在,如果使用输入"hi"提交表单,则返回

------WebKitFormBoundaryVs2BrJjCaLCWNF9o
Content-Disposition: form-data; name="value"

hi
------WebKitFormBoundaryVs2BrJjCaLCWNF9o--

这告诉我应该有办法获取字符串值,但我能看到的唯一方法是手动解析它。

(另外,我试图弄脏一点,只使用JDK附带的库来实现这个服务器。我已经在工作中使用了Apache服务器,但那并非如此。我想在这里做什么。)

1 个答案:

答案 0 :(得分:0)

public Server() throws IOException {
    HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
    server.createContext("/", new HttpHandler() {
        @Override
        public void handle(HttpExchange t) throws IOException {
            StringBuilder sb = new StringBuilder();
            InputStream ios = t.getRequestBody();
            int i;
            while ((i = ios.read()) != -1) {
                sb.append((char) i);
            }
            System.out.println("hm: " + sb.toString());

            String response = " <html>\n"
                    + "<body>\n"
                    + "\n"
                    + "<form action=\"http://localhost:8000\" method=\"post\">\n"
                    + "input: <input type=\"text\" name=\"input\"><br>\n"
                    + "input2: <input type=\"text\" name=\"input2\"><br>\n"
                    + "<input type=\"submit\">\n"
                    + "</form>\n"
                    + "\n"
                    + "</body>\n"
                    + "</html> ";
            t.sendResponseHeaders(200, response.length());
            OutputStream os = t.getResponseBody();
            os.write(response.getBytes());
            os.close();

        }
    });
    server.setExecutor(null);
    server.start();
}

不确定是否要这样做,但这会返回所有帖子页面中包含名称和值的字符串

喜欢

input=value&input2=value2&......