0

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

[1]: http://localhost:8001/test?query=SELECT * WHERE{ ?s rdf:type ?o. }

我总是收到这样的回复:

400 Bad Request
URISyntaxException thrown

这是我的服务器的代码:

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();
        }
    }
}

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

4

2 回答 2

5

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

于 2011-12-16T13:09:57.887 回答
5

是的,某些字符在 url 的某些部分无效。(除了我希望你只是在测试它而不是实际将它用于任何真实的事情,sql 注入攻击和所有),你需要先使用 URLEncoder 对 sql 查询进行编码。

于 2011-12-16T13:10:05.137 回答