简体   繁体   中英

How can I make HttpServer recognise special characters in Java?

I want to make some SQL query with my Java HttpServer but it seems that the HttpServer doesn't recognize special characters in the link I submit into my browser:

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

I always receive this response:

400 Bad Request
URISyntaxException thrown

This is the code for my server:

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

How can I fix this?

yes, there are certain characters which are not valid in certain parts of a url. ( besides the fact that i hope you are just testing this and not actually using it for anything real, sql injection attacks and all ), you need to use URLEncoder to encode the sql query first.

您永远不要在URL中传递SQL查询。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM