Android > HttpURLConnection - Method
µî·ÏÀÏ : 2017-07-06 17:34
Á¶È¸¼ö : 53,415
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());
// ij½¬ »ç¿ë ¿©ºÎ¿Í µðÆúÆ® °ª
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