首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何解决无法在java中通过http读取客户端请求?

如何解决无法在java中通过http读取客户端请求?
EN

Stack Overflow用户
提问于 2018-10-17 02:07:04
回答 1查看 0关注 0票数 0

有一个GPS设备,它通过给定端口通过http连接,以便从http服务器发送和接收数据。出于测试目的,我创建了一个简单的java http服务器,它读取请求并发送响应。这是我的代码:

代码语言:javascript
复制
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信息的示例:

在此处输入图像描述
在此处输入图像描述
EN

回答 1

Stack Overflow用户

发布于 2018-10-17 12:04:18

HTTP通过TCP运行。话说回来可以在这里做几件事来调试这种情况:

  • 远程调试服务器
  • 尝试使用像邮递员这样的应用程序向服务器发送请求,还会显示反应
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100002922

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档