안규 공부방

Android > HttpURLConnection - Method

등록일 : 2017-07-06 17:34 조회수 : 94,029

HttpURLConnection http = (HttpURLConnection) url.openConnection();
메소드 설 명
setConnectionTimeout(10000) 연결 대기 시간 설정 (10초 동안)
setRequestMethod("POST"); POST방식으로 요청한다.( 기본값은 GET )
setDoOutput(true); OutputStream으로 POST 데이터를 넘겨주겠다는 옵션을 정의한다.
setDoInput(true); InputStream으로 서버로 부터 응답 헤더와 메시지를 읽어들이겠다는 옵션을 정의한다.
setRequestProperty(); 요청 헤더를 정의한다.
setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
Object getContent() content객체를 반환한다.
String getContentEncoding() content객체의 인코딩을 반환한다.
int getContentLength() content의 크기를 반환한다.
int getContentType() content의 type을 반환한다.
getOutputStream(); 새로운 OutputStream에 요청할 OutputStream을 넣는다.
OutputStream os = http.getOutputStream();
getResponseCode(); 웹서버에 요청과정 수행
int resCode = http.getResponseCode();
	
	 
	 
	 

	예제)
	
	URL url = null;
	String address = "http://www.javachobo.com/sample/hello.html";
	String line = "";
	 
	try{
		url = new URL(address);
		URLConnection conn = url.openConnection();
		 
		System.out.println("conn.toString() : " + conn);
		 
		// UserInteraction의 허용여부 반환
		System.out.println("getAllowUserInteraction() : " + conn.getAllowUserInteraction());
		 
		// 연결 종료 시간(ms)
		System.out.println("getConnectTimeout() : " + conn.getConnectTimeout());
		 
		// Content 객체
		System.out.println("getContent() : " + conn.getContent());
		System.out.println("getContentEncoding() : " + conn.getContentEncoding());
		System.out.println("getContentLength() : " + conn.getContentLength());
		System.out.println("getContentType() : " + conn.getContentType());
		 
		// Header의 date필드값 반환
		System.out.println("getDate() : " + conn.getDate());
		 
		// DefaultUserInteraction 반환
		System.out.println("getDefaultAllowUserInteraction() : " + conn.getDefaultAllowUserInteraction());
		 
		// 캐쉬 사용 여부와 디폴트 값
		System.out.println("getUseChaches() : " + conn.getUseCaches());
		System.out.println("getDefaultUserCaches() : " + conn.getDefaultUseCaches());
		 
		// DoInput/DoOutput 필드값 반환
		System.out.println("getDoInput() : " + conn.getDoInput());
		System.out.println("getDoOutput() : " + conn.getDoOutput());
		 
		// 각 필드값
		System.out.println("getExpiration() : " + conn.getExpiration());
		System.out.println("getHeaderFields() : " + conn.getHeaderFields());
		System.out.println("getIfModifiedSince() : " + conn.getIfModifiedSince());
		System.out.println("getLastModified() : " + conn.getLastModified());
		System.out.println("getURL() : " + conn.getURL());
		 
	}catch (Exception e) {
		e.printStackTrace();
	}
	
결과)
	conn.toString() : sun.net.www.protocol.http.HttpURLConnection:http://www.javachobo.com/sample/hello.html
	getAllowUserInteraction() : false
	getConnectTimeout() : 0
	getContent() : sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@a59698
	getContentEncoding() : null
	getContentLength() : 174
	getContentType() : text/html
	getDate() : 1326353353000
	getDefaultAllowUserInteraction() : false
	getUseChaches() : true
	getDefaultUserCaches() : true
	etDoInput() : true
	getDoOutput() : false
	getExpiration() : 0
	getHeaderFields() : {null=[HTTP/1.1 200 OK], ETag=["12e391-ae-46dad401"], Date=[Thu, 12 Jan 2012 07:29:13 GMT], 
		Content-Length=[174], Last-Modified=[Sun, 02 Sep 2007 15:17:21 GMT], Keep-Alive=[timeout=5, max=60], 
		Accept-Ranges=[bytes], 
		Connection=[Keep-Alive], Content-Type=[text/html], Server=[RC-Web Server]}
	getIfModifiedSince() : 0
	getLastModified() : 1188746241000
	getURL() : http://www.javachobo.com/sample/hello.html
※ 혹시 도움이 되셨다면 댓글에 한마디 남겨주세요!
작성자   비밀번호
자동글 방지     (자동글 방지 기능입니다.)
내용   댓글달기 
이메일 문의 : cak0280@nate.com  
Copyright 2000 By ENTERSOFT.KR All Rights Reserved.