import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class TestMain { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub try { URL url = new URL("http://www.baidu.com"); System.out.println(url.getHost()); HttpURLConnection urlConnection =(HttpURLConnection) url.openConnection(); urlConnection.setConnectTimeout(20000); urlConnection.setRequestMethod("GET"); urlConnection.setDoOutput(true); urlConnection.connect(); //InputStream in = urlConnection.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); System.out.println(in.readLine()); System.out.println(in.readLine()); System.out.println(in.readLine()); urlConnection.disconnect(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
时间: 2024-10-03 05:06:23