java访问网页

  try {

    String getURL = "http://221.182.1.4/apm_internal/sdapi_2/testpj.php"; 

 

            URL getUrl = new URL(getURL); 

 

            // 根据拼凑的URL,打开连接,URL.openConnection()函数会根据 URL的类型,返回不同的URLConnection子类的对象,在这里我们的URL是一个http,因此它实际上返回的是HttpURLConnection 

 

            HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection(); 

 

            // 建立与服务器的连接,并未发送数据 

connection.connect();

// 发送数据到服务器并使用Reader读取返回的数据 

 

        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 

 

        System.out.println(" ============================= "); 

 

        System.out.println(" Contents of get request "); 

 

        System.out.println(" ============================= "); 

 

        String lines; 

 

        while ((lines = reader.readLine()) != null) { 

 

                System.out.println(lines); 

 

        } 

 

        reader.close(); 

 

        // 断开连接 

 

        connection.disconnect(); 

 

        System.out.println(" ============================= "); 

 

        System.out.println(" Contents of get request ends "); 

 

        System.out.println(" ============================= "); 

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

 

 

 

 

  JOptionPane.showMessageDialog(null, "发送消息", "消息提示", JOptionPane.ERROR_MESSAGE);

  return true;

posted @ 2015-03-19 11:25  宝贝,我永远都在  阅读(101)  评论(0)    收藏  举报