码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA_HttpURLConnection_客户端接收数据

时间:2014-12-04 10:08:38      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   ar   os   java   for   on   数据   

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_客户端接收数据

标签:blog   http   io   ar   os   java   for   on   数据   

原文地址:http://www.cnblogs.com/SKeyC27/p/4141981.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!