【问题标题】:Get the same output of Netcat in Java在 Java 中获得与 Netcat 相同的输出
【发布时间】:2016-06-19 06:36:04
【问题描述】:

我希望获得与在终端中(使用 Netcat)执行此操作时相同的输出(Java 中):

nc -l localhost 8080

我得到的输出是:

GET /?code=Mdf81e469-fca0-f04e-db95-278aff0323be HTTP/1.1
Host: localhost:8080
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de-de
Connection: keep-alive
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/601.4.4 (KHTML, like Gecko) Version/9.0.3 Safari/601.4.4

我需要这个,因为GET 之后第一行的输出。我想将它作为 Java 中的 Stringvalue。

【问题讨论】:

    标签: java sockets port serversocket netcat


    【解决方案1】:

    您可以使用Runtime 类的exec 方法执行此命令并取回结果。这是一个小示例,您可以根据需要进行修改:

      String[] commands = new String[]{"nc", "localhost", "8080"};
      Process childProc = Runtime.getRuntime().exec(command);
      InputStream in = childProc.getInputStream();
      int c;
      while ((c = in.read()) != -1) 
      {
        System.out.print((char)c);
      }
      in.close();
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      • 2014-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-01
      • 1970-01-01
      相关资源
      最近更新 更多