java inputstream 打印以控制内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1083674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:23:06  来源:igfitidea点击:

java inputstream print to console the content

javaservletshttpwebrequest

提问by cometta

sock = new Socket("www.google.com", 80);
       out  = new BufferedOutputStream(sock.getOutputStream());
       in   = new BufferedInputStream(sock.getInputStream());

When i try to do printing out of content inside "in" like below

当我尝试打印出“in”内的内容时,如下所示

 BufferedInputStream bin = new BufferedInputStream(in);
 int b;
 while ( ( b = bin.read() ) != -1 )
 {

     char c = (char)b;         

     System.err.print(""+(char)b); //This prints out content that is unreadable.
                                   //Isn't it supposed to print out html tag?
 }

回答by David Rabinowitz

If you want to print the content of a web page, you need to work with the HTTPprotocol. You do not have to implement it yourself, the best way is to use existing implementations such as the java API HttpURLConnectionor Apache's HttpClient

如果要打印网页的内容,则需要使用HTTP协议。您不必自己实现它,最好的方法是使用现有的实现,例如 java API HttpURLConnection或 Apache 的HttpClient

Here is an example of how to do it with HttpURLConnection:

以下是如何使用 HttpURLConnection 执行此操作的示例:

URL url = new URL("http","www.google.com");
HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
urlc.setAllowUserInteraction( false );
urlc.setDoInput( true );
urlc.setDoOutput( false );
urlc.setUseCaches( true );
urlc.setRequestMethod("GET");
urlc.connect();
// check you have received an status code 200 to indicate OK
// get the encoding from the Content-Type header
BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
String line = null;
while((line = in.readLine()) != null) {
  System.out.println(line);
}

// close sockets, handle errors, etc.

As written above, you can save traffic by adding the Accept-Encoding header and check the Content-Encoding header of the response.

如上所述,您可以通过添加 Accept-Encoding 标头并检查响应的 Content-Encoding 标头来节省流量。

Here is an HttpClient Example, taken from here:

这是一个 HttpClient 示例,取自此处

   // Create an instance of HttpClient.
    HttpClient client = new HttpClient();

    // Create a method instance.
    GetMethod method = new GetMethod(url);

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
            new DefaultHttpMethodRetryHandler(3, false));

    try {
      // Execute the method.
      int statusCode = client.executeMethod(method);

      if (statusCode != HttpStatus.SC_OK) {
        System.err.println("Method failed: " + method.getStatusLine());
      }

      // Read the response body.
      byte[] responseBody = method.getResponseBody();

      // Deal with the response.
      // Use caution: ensure correct character encoding and is not binary data
      System.out.println(new String(responseBody));

    } catch (HttpException e) {
      System.err.println("Fatal protocol violation: " + e.getMessage());
      e.printStackTrace();
    } catch (IOException e) {
      System.err.println("Fatal transport error: " + e.getMessage());
      e.printStackTrace();
    } finally {
      // Release the connection.
      method.releaseConnection();
    }  

回答by Tim Büthe

If you what to fetch the content of a webpage, you should take a look at apache httpclientinstead of coding this yourself, expect for learning purposes or any other really good reason.

如果您要获取网页的内容,您应该看看apache httpclient而不是自己编写代码,期望用于学习目的或任何其他真正好的理由。

回答by gorefest

Very easy to create a String from a Stream using Java 8 Stream API:

使用 Java 8 Stream API 从流创建字符串非常容易:

new BufferedReader(new InputStreamReader(in)).lines().collect(Collectors.joining("\n"))

Using IntelliJ I even can set this beeing a debug expression: enter image description here

使用 IntelliJ 我什至可以将此 bee 设置为调试表达式: 在此处输入图片说明

I guess in Eclipse it will work similar.

我想在 Eclipse 中它的工作方式类似。