⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 urlconnectiondemo.java

📁 Java网络编程应用:包括简易聊天程序的客服端和服务器端的源代码
💻 JAVA
字号:
import java.io.*;
import java.net.*;
import java.util.*;

/**
   This program connects to an URL and displays the 
   response header data and the requested data. 
*/
public class URLConnectionDemo
{  
   public static void main(String[] args)
   {
   	URLConnection connection = null;
   	URL url = null;
	String urlName;
	if (args.length > 0)
		urlName = args[0];
	else
		urlName = "http://java.sun.com";
	try{
		url = new URL(urlName);
	}catch(MalformedURLException me){}
	try{
		connection = url.openConnection();
		connection.connect();
	}catch (IOException e){}
	
	// print header fields
	int n = 1;
	String key;
	while ((key = connection.getHeaderFieldKey(n)) != null){  
		String value = connection.getHeaderField(n);
		System.out.println(key + ": " + value);
		n++;
	}
	
	// print convenience functions
/*	System.out.println("----------");
	System.out.println("getContentType: "
		+ connection.getContentType());
	System.out.println("getContentLength: "
		+ connection.getContentLength());
	System.out.println("getContentEncoding: "
		+ connection.getContentEncoding());
	System.out.println("getDate: "
		+ connection.getDate());
	System.out.println("getExpiration: "
		+ connection.getExpiration());
	System.out.println("getLastModifed: "
		+ connection.getLastModified());
	System.out.println("----------");  */
	try{	
		BufferedReader in = new BufferedReader(new
		InputStreamReader(connection.getInputStream()));
		// print lines of contents
		String line;
		while ((line = in.readLine()) != null){  
			System.out.println(line);
		}
	}catch (IOException e){e.printStackTrace();}
	System.out.println("----------");
	System.out.println("getContentType: "
		+ connection.getContentType());
	System.out.println("getContentLength: "
		+ connection.getContentLength());
	System.out.println("getContentEncoding: "
		+ connection.getContentEncoding());
	System.out.println("getDate: "
		+ connection.getDate());
	System.out.println("getExpiration: "
		+ connection.getExpiration());
	System.out.println("getLastModifed: "
		+ connection.getLastModified());
	System.out.println("----------");
   }
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -