Content Negotiation Test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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();
			}
		}
 
	}
}