【发布时间】:2013-04-29 13:09:33
【问题描述】:
我有两个 HttpURLConnections 从客户端连接到服务器。这不是典型的 HttpServer,我正在编写客户端和服务器的代码,但只能使用端口 80/443(用于测试任何端口的工作原理)。第一个请求顺利通过,但第二个请求(在第一个请求之后立即发生)无法到达服务器。我正在调用HttpExchange.close 和HttpURLConnection.disconnect 并刷新所有流,但我无法获得第二个到达服务器的请求。我正在使用 Oracle 的 JDK 1.7_17 x64 并在 Win7 x64 上运行。下面是 SSCCE 的输出和 SSCCE。我错过了什么!!??
服务器
Started server @ /127.0.0.1:8765
Handling HttpExchange!
Processing request from user: joe.smith
客户:
ClientID: 0
Response code: 200
这里是 SSCCE:
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.URL;
import javax.swing.JOptionPane;
public class CommsTest {
public static void main(String[] args) throws IOException {
Object startMe = JOptionPane.showInputDialog(null, "Start client or server?", "Launcher", JOptionPane.QUESTION_MESSAGE, null,
new Object[]{"Client", "Server"}, "Server");
if (startMe.equals("Server")) {
HttpServer httpServer = HttpServer.create(new InetSocketAddress("localhost", 8765), 50000);
httpServer.createContext("/test", new HttpHandler() {
private byte clientIdCounter = 0;
@Override
public void handle(HttpExchange he) throws IOException {
System.out.println("Handling HttpExchange!");
int requestCode = Integer.parseInt(he.getRequestHeaders().getFirst("ReqCode"));
switch (requestCode) {
case 0:
// request '0' sends username and accepts a byte back
try (ObjectInputStream ois = new ObjectInputStream(he.getRequestBody())) {
System.out.println("Processing request from user: " + ois.readUTF());
}
// send a response of code of 200, all is well! and i'm sending 1 byte back.
he.sendResponseHeaders(200, 1);
he.getResponseBody().write(new byte[]{clientIdCounter++});
he.close();
break;
case 1:
// request '1' sends 3 bytes
try (ObjectInputStream ois = new ObjectInputStream(he.getRequestBody())) {
byte clientId = ois.readByte();
byte opCode = ois.readByte();
byte argument = ois.readByte();
System.out.println(String.format("Received op request %d from client %d with argument %d", clientId, opCode,
argument));
// send a response code of 200, all is well! no data is being sent back though.
he.sendResponseHeaders(200, 0);
he.close();
}
break;
}
}
});
httpServer.start();
System.out.println("Started server @ "+ httpServer.getAddress());
} else if (startMe.equals("Client")) {
// first phase of the connection, send a opCode of 0, send the username, receive the client id
HttpURLConnection con = create(true, true, 0);
try (ObjectOutputStream oos = new ObjectOutputStream(con.getOutputStream())) {
oos.writeUTF(System.getProperty("user.name"));
}
byte clientId = (byte) con.getInputStream().read();
System.out.println("ClientID: " + clientId);
System.out.println("Response code: "+ con.getResponseCode());
con.disconnect();
// next phase of connection, send an opCode of 1 and 3 bytes, receive nothing
con = create(false, true, 0);
try (ObjectOutputStream oos = new ObjectOutputStream(con.getOutputStream())) {
oos.writeByte(0);
oos.writeByte(4);
oos.writeByte(2);
oos.flush(); //I've also tried closing the oos manually and calling con.disconnect, to no avail
}
}
}
private static HttpURLConnection create(boolean input, boolean output, int reqCode) throws IOException {
URL url = new URL("http://localhost:8765/test");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setUseCaches(false);
//I have tried #setRequestMethod("POST") but that didn't help
con.setDoInput(input);
con.setDoOutput(output);
con.setRequestProperty("ReqCode", Integer.toString(reqCode));
return con;
}
}
【问题讨论】:
-
看起来问题与发回响应标头有关,例如200 可以。这显然算作响应数据(我认为标题只是一个标题,而不是真正的数据)。无论如何,我仍在努力弄清楚如何正确地清理它 - 如果在这个 HTTP 世界中出现任何问题,它就会悄悄地下山!如果/当我把它全部清理干净时,我会发布 SSCCE。
标签: java networking httpurlconnection httpserver