集册 Java实例教程 从URL获取文本

从URL获取文本

欢马劈雪     最近更新时间:2020-01-02 10:19:05

527
从URL获取文本

import java.io.BufferedReader;/** 来 自 时 代 J a v a 公 众 号**/

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.MalformedURLException;

import java.net.URL;


public class Main {

  public static void main(String[] args) {

    try {

      // Create a URL for the desired page

      URL url = new URL("http://hostname:80/index.html");


      // Read all the text returned by the server

      BufferedReader in = new BufferedReader(new InputStreamReader(

          url.openStream()));//from nowjava.com

      String str;

      while ((str = in.readLine()) != null) {

        
展开阅读全文