簡體   English   中英

無法通過Java中的HTTP讀取客戶端請求

[英]Unable to read client request over http in java

我有一個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信息的示例: 在此處輸入圖片說明

HTTP通過TCP運行。 話說回來。 您可以在此處做幾件事來調試這種情況:

  • 遠程調試服務器
  • 嘗試使用郵遞員之類的應用程序將請求發送到您的服務器。 它還將顯示您得到的響應。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM