Content Negotiation Test


			
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;

public class ContentNegotiationTest {
	public static void main(String[] args) throws IOException {
		
		String uri = null;
		String contentType = null;
		
		uri = "http://lod.nl.go.kr/resource/KMO200932143";
		contentType = "text/html";
		testURI(uri, contentType);	
		contentType = "application/rdf+xml";
		testURI(uri, contentType);
	}
	public static void testURI(String uri, String contentType){
		
		System.out.println("[Header]==============================================================");
		BufferedReader br = null;
		URL url;
		try {
			url = new URL(uri);
			HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();			
			httpConn.setRequestProperty("Accept",contentType);
			
			Map map = httpConn.getHeaderFields();
			Iterator iter = map.keySet().iterator();
			
			while(iter.hasNext()){
				String key = (String) iter.next();
				System.out.println(key + "\t" + map.get(key));
				
			}
			System.out.println("========================================================");
			System.out.println("["+contentType+"========================================");
			br = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
			String line = null;
			int i = 0;
			while((line = br.readLine()) != null){
				System.out.println(line);
				i++;
				if(i == 10) break; // 전체를 보고 싶으면 주석처리 하세요.
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}	finally{
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
}