有一个GPS设备,它通过给定端口通过http连接,以便从http服务器发送和接收数据。出于测试目的,我创建了一个简单的java http服务器,它读取请求并发送响应。这是我的代码:
public class simpleHTTPServer {
public static void main(String[] args) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(9090), 0);
HttpContext context = server.createContext("/");
context.setHandler(simpleHTTPServer::handleRequest);
server.start();
}
private static void handleRequest(HttpExchange exchange) throws IOException {
// URI requestURI = exchange.getRequestURI();
printRequestInfo(exchange);
System.out.println("sending command..");
String response = "123456";
exchange.sendResponseHeaders(200, response.getBytes().length);
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
}
private static void printRequestInfo(HttpExchange exchange) {
System.out.println("-- headers --");
Headers requestHeaders = exchange.getRequestHeaders();
requestHeaders.entrySet().forEach(System.out::println);
System.out.println("-- principle --");
HttpPrincipal principal = exchange.getPrincipal();
System.out.println(principal);
System.out.println("-- HTTP method --");
String requestMethod = exchange.getRequestMethod();
System.out.println(requestMethod);
System.out.println("-- query --");
URI requestURI = exchange.getRequestURI();
String query = requestURI.getQuery();
System.out.println(query);
}
}
实际上这个设备确认它连接了一个http服务器(http:// mydomain:9090)并不断发送数据,但在我运行的http服务器程序中,我无法从该设备获得任何请求。
为了验证这一点,我在端口9090上使用了tcpdump,并使用Wireshark验证了该设备是否成功发送了请求。我有点困惑,因为Wireshark显示请求来自TCP协议。那我怎么能在java中通过http读取这个请求呢?
以下是wireshark信息的示例:
发布于 2018-10-17 12:04:18
HTTP通过TCP运行。话说回来可以在这里做几件事来调试这种情况:
https://stackoverflow.com/questions/-100002922
复制相似问题