对URL的使用

来源:互联网 发布:美国钻井平台数据最新 编辑:程序博客网 时间:2024/06/04 13:18

这里写图片描述

实现代码:

import java.net.*;import java.util.*;import java.io.*;public class GetGoogle {   public static void main(String[] args) throws Exception {      // TODO Auto-generated method stub      System.out.println("获取日文页面");      getContentByLanguage("ja");      System.out.println("\n");      System.out.println("获取中文页面");      getContentByLanguage("zh-cn");   }   public static void getContentByLanguage(String country) throws Exception   {      URL urlGoogle = new URL("http://www.google.com");      HttpURLConnection googleConnection =            (HttpURLConnection)urlGoogle.openConnection();      googleConnection.setRequestProperty("Accept-Language",country);      Map requests = googleConnection.getRequestProperties();      Set reqFields = requests.keySet();      Iterator itrReq = reqFields.iterator();      while (itrReq.hasNext()) {         String field = (String)itrReq.next();         System.out.println(field + ":" +               googleConnection.getRequestProperty(field));      }      googleConnection.connect();      Map responses = googleConnection.getHeaderFields();      Set resFields = responses.keySet();      Iterator itrRes = resFields.iterator();      while (itrRes.hasNext()) {         String field = (String)itrRes.next();         System.out.println(field + ":" +               googleConnection.getHeaderField(field));      }      InputStream is = googleConnection.getInputStream();      BufferedReader br = new BufferedReader(            new InputStreamReader(is));      String strLine = null;      while ((strLine = br.readLine()) != null) {         System.out.println(strLine);      }      br.close();      googleConnection.disconnect();   }}
0 0
原创粉丝点击