如何让HttpServer识别Java中的特殊字符?

0 java simplehttpserver httpserver

我想用我的Java HttpServer进行一些SQL查询,但似乎HttpServer无法识别我提交到浏览器的链接中的特殊字符:

[1]: http://localhost:8001/test?query=SELECT * WHERE{ ?s rdf:type ?o. }
Run Code Online (Sandbox Code Playgroud)

我总是收到这样的回复:

400 Bad Request
URISyntaxException thrown
Run Code Online (Sandbox Code Playgroud)

这是我的服务器的代码:

public class SimpleHttpServer 
{

public static void main(String[] args) throws Exception 
{

        dir = args[0];
        HttpServer server = HttpServer.create(new InetSocketAddress(8001), 0);
        server.createContext("/test", new MyHandler());
        server.setExecutor(null);
        server.start();
    }

    static class MyHandler implements HttpHandler {
        public void handle(HttpExchange t) throws IOException {
            String response ;
            System.out.println(t.getRequestURI().getQuery().toString().replaceAll("query=",   ""));
            response =   ExecuteHttpQuery.getInstance().httpQuery(t.getRequestURI().getQuery().toString().replaceAll("query=", "").toString(), dir) + "\n";
            t.sendResponseHeaders(200, response.length());
            OutputStream os = t.getResponseBody();
            os.write(response.getBytes());
            os.close();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题?

LaG*_*ere 5

你永远不应该在你的 URL 中传递你的 SQL 查询。


jta*_*orn 5

是的,某些字符在网址的某些部分无效.(除了我希望你只是测试这个并且实际上并没有将它用于任何真实的,sql注入攻击等),你需要首先使用URLEncoder对sql查询进行编码.