欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

一个简单的无需第三方包的获取网页内容的代码,第三方网页内容,直接用 Java 自带的

来源: javaer 分享于  点击 17341 次 点评:232

一个简单的无需第三方包的获取网页内容的代码,第三方网页内容,直接用 Java 自带的


直接用 Java 自带的包搞定,无需其他第三方jar包

[Java]代码

public static String fetch_url(String url) throws IOException {      BufferedReader bis = null;      InputStream is = null;      try {          URLConnection connection = new URL(url).openConnection();          is = connection.getInputStream();          // warning of UTF-8 data          bis = new BufferedReader(new InputStreamReader(is, "UTF-8"));          String line = null;          StringBuffer result = new StringBuffer();          while ((line = bis.readLine()) != null) {              result.append(line);          }          return result.toString();      } finally {          if (bis != null) {              try {                  bis.close();              } catch (IOException e) {                  e.printStackTrace();              }          }          if (is != null) {              try {                  is.close();              } catch (IOException e) {                  e.printStackTrace();              }          }      }  }  
相关栏目:

用户点评