首页 > 代码库 > JAVA_HttpURLConnection_客户端接收数据

JAVA_HttpURLConnection_客户端接收数据

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(); } }}

  

JAVA_HttpURLConnection_客户端接收数据