我想使用 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();
}
}
}
我该如何解决这个问题?
最佳答案
您绝对不应该在 URL 中传递 SQL 查询。
关于java - 如何让HttpServer识别Java中的特殊字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8534577/