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

  

转载于:https://www.cnblogs.com/SKeyC27/p/4141981.html

你可能感兴趣的:([JAVA]JAVA_HttpURLConnection_客户端接收数据)