httpurlconnection
场景:HttpUrlConnection 应用
HttpUrlConnection 使用
HttpUrlConnection 使用
package com.longtop; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import sun.net.www.protocol.http.HttpURLConnection; //注意: 这里的HttpURLConnection 类所属的包是sun.net.www.protocol.http public class HTTPtest { public static void main(String[] args) { try { URL url = new URL("http://www.baidu.com"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader( connection.getInputStream(),"GBK")); String lines = null; while ((lines = reader.readLine()) != null) { System.out.println(lines); } reader.close(); // 断开连接 connection.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }