我使用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();