본문 바로가기

Android

Android HttpUrlConnection Request

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
64
65
66
67
// HttpURLConnection 객체 생성.
HttpURLConnection conn = null;
 
// URL 연결 (웹페이지 URL 연결.)
conn = (HttpURLConnection)url.openConnection();
 
// TimeOut 시간 (서버 접속시 연결 시간)
conn.setConnectTimeout(CONN_TIMEOUT * 1000);
 
// TimeOut 시간 (Read시 연결 시간)
conn.setReadTimeout(READ_TIMEOUT * 1000);
 
// 요청 방식 선택 (GET, POST)
conn.setRequestMethod(GET);
 
// Request Header값 셋팅 setRequestProperty(String key, String value)
conn.setRequestProperty("NAME""name");
conn.setRequestProperty("MDN""mdn");
conn.setRequestProperty("APPID""appid");
 
// 서버 Response Data를 xml 형식의 타입으로 요청.
conn.setRequestProperty("Accept""application/xml");
 
// 서버 Response Data를 JSON 형식의 타입으로 요청.
conn.setRequestProperty("Accept""application/json");
 
// 타입설정(text/html) 형식으로 전송 (Request Body 전달시 text/html로 서버에 전달.)
conn.setRequestProperty("Content-Type""text/html");
 
// 타입설정(text/html) 형식으로 전송 (Request Body 전달시 application/xml로 서버에 전달.)
conn.setRequestProperty("Content-Type""application/xml");
 
// 타입설정(application/json) 형식으로 전송 (Request Body 전달시 application/json로 서버에 전달.)
conn.setRequestProperty("Content-Type""application/json");
 
// 컨트롤 캐쉬 설정
conn.setRequestProperty("Cache-Control","no-cache");
 
// 타입길이 설정(Request Body 전달시 Data Type의 길이를 정함.)
conn.setRequestProperty("Content-Length""length")
 
// User-Agent 값 설정
conn.setRequestProperty("User-Agent""test");
 
// OutputStream으로 POST 데이터를 넘겨주겠다는 옵션.
conn.setDoOutput(true);
 
// InputStream으로 서버로 부터 응답을 받겠다는 옵션.
conn.setDoInput(true);
 
// Request Body에 Data를 담기위해 OutputStream 객체를 생성.
OutputStream os = conn.getOutputStream();
 
// Request Body에 Data 셋팅.
os.write(body.getBytes("euc-kr"));
 
// Request Body에 Data 입력.
os.flush();
 
// OutputStream 종료.
os.close();
 
// 실제 서버로 Request 요청 하는 부분. (응답 코드를 받는다. 200 성공, 나머지 에러)
int responseCode = conn.getResponseCode();
 
// 접속해지
conn.disconnect();
cs