没有参数的查询HttpServer总是从HttpServer返回java.net.SocketException

时间:2018-04-08 10:58:05

标签: java httpserver

我使用com.sun.net.httpserver.HttpServer创建了一个服务器。当URL包含参数时,服务器正常工作。但是,当URL不包含任何参数时,我从服务器获取java.net.SocketException: Unexpected end of file

我使用以下代码发出请求:

URL serverUrl = new 
URL("http://localhost:"+MockServer.getInstance().getPort()+"/");
HttpURLConnection httpURLConnection =(HttpURLConnection)serverUrl.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setAllowUserInteraction(false);
httpURLConnection.setUseCaches(false);
int responseCode = httpURLConnection.getResponseCode();

但是,当我向URL添加参数时(如下面一行所示),没有异常,然后我得到responseCode=200预期结果

URL serverUrl = new URL("http://localhost:"+MockServer.getInstance().getPort()+"/?x=y");

服务器中的代码:

httpServer = HttpServer.create( new InetSocketAddress(0),0);
httpServer.createContext("/", new MockHandler());
httpServer.setExecutor(null);
httpServer.start();

0 个答案:

没有答案