sitelink1 http://swlock.blogspot.kr/2015/03/httpurlconnection.html 
sitelink2  
sitelink3  

앞에서 HttpURLConnection 이용하여 웹페이지의 내용을 읽어오는 방법에 대해서 알아 보았습니다. 문제는 로그인 페이지가 있거나 하면 세션이 유지가 안되는 현상이 있어서 일부 site에서 사용하기에는 문제가 있었습니다.
그래서 기존 작성 코드에 쿠키를 저장하는 코드를 추가하였습니다.
saveCookie() 함수와 저장된 쿠키의 값을 전달해주는 부분이 변경 내용입니다. conn.setRequestProperty( "Cookie", m_cookies )

변경된 전체 소스를 공유합니다.

 

package com.example.zdgtest2;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map;

import android.util.Log;

public class GetHttp {
 final static double VERSION = 1.20141016;
 static final int ERROR_CODE_HTTP_NOT_FOUND = -404;
 static final int ERROR_CODE_HTTP_UNAUTHORIZED = -401;
 static final int ERROR_CODE_HTTP_ELSE = -1;
 static final int ERROR_CODE_HTTP_EXCEPTION = -1000;
 static final int ERROR_CODE_NOERROR = 0;
 byte[] htmlByte = null;
 int errorCode = 0;
 boolean bUseCache = true;
 boolean m_session = false;
 String m_cookies = "";
 public String getString() {
  try {
   return new String(htmlByte, "UTF-8");
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }
  return null;
 }
 public byte[] getByte() {
  return htmlByte;  
 }
 public int getErrorCode() {
  return errorCode;
 }
 public boolean execute(String addr) {
  return execute(addr, 5, 10);
 }
 public void saveCookie( HttpURLConnection conn)
 {
  
     Map<String, List<String>> imap = conn.getHeaderFields( ) ;
     if( imap.containsKey( "Set-Cookie" ) )
     {
   List<String> lString = imap.get( "Set-Cookie" ) ;
   for( int i = 0 ; i < lString.size() ; i++ ) {
    m_cookies += lString.get( i ) ;
   }
   Log.e("zdg",m_cookies);
         m_session = true ;
     } else {
      m_session = false ;
     }
 }
 // 참고 소스:http://markan82.tistory.com/32
 public boolean execute(String addr, int connTimeOutSec, int readTimeOutSec) {
  boolean retval = true;
  errorCode = ERROR_CODE_NOERROR;
  try {
   URL url = new URL(addr);
   Log.e("zdg",addr);
   HttpURLConnection conn = (HttpURLConnection) url.openConnection();
   if( conn != null ) {
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
    if( m_session ) {
     conn.setRequestProperty( "Cookie", m_cookies );
    }
    conn.setConnectTimeout(1000*connTimeOutSec);
    conn.setReadTimeout(1000*readTimeOutSec);
    conn.setUseCaches(bUseCache);
    conn.setDoInput(true);

    int resCode = conn.getResponseCode();
    saveCookie(conn);

    if( resCode == HttpURLConnection.HTTP_OK ) {
     htmlByte = inputStreamToByte(conn.getInputStream());
     if(htmlByte == null || htmlByte.length == 0){
      errorCode = ERROR_CODE_HTTP_ELSE;
      retval = false;
     }
    } else if( resCode == HttpURLConnection.HTTP_NOT_FOUND ) {
     errorCode = ERROR_CODE_HTTP_NOT_FOUND;
     retval = false;
    } else if( resCode == HttpURLConnection.HTTP_UNAUTHORIZED ) {
     errorCode = ERROR_CODE_HTTP_UNAUTHORIZED;
     retval = false;
    } else {
     errorCode = ERROR_CODE_HTTP_ELSE;
     retval = false;
    }
    // DISCONNECT 
    conn.disconnect();
   } else {
    errorCode = ERROR_CODE_HTTP_ELSE;
    retval = false;
   }
  } catch(Exception e) {
   e.printStackTrace();
   errorCode = ERROR_CODE_HTTP_EXCEPTION;
   retval = false;
  }
  return retval;
 }
 private byte[] inputStreamToByte(InputStream in)
 {
  final int BUF_SIZE = 1024; 
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  byte[] buffer = new byte[BUF_SIZE];
  try {
   int length;
   while ((length = in.read(buffer)) != -1) out.write(buffer, 0, length);
  } catch (IOException e) {
   e.printStackTrace();
   return null;
  }
  return out.toByteArray();
 }
}



다음은 호출하는곳의 예제 코드입니다.

 

 private void webTest() {
  Thread nthread = new Thread(new Runnable() {
    public void run() {
     try {
      GetHttp html = new GetHttp();
    if(html.execute("http://xxx:8080/jsp/admin/user/userCtrl.jsp?id=xxx&pass=xxx",30,30)!=true)
    {
     printLog("error "+html.errorCode);
    }
    if ( html.getByte() != null && html.getByte().length > 0 ){
     Utils.fileWriteByte("/sdcard/Download/test1.html",html.getByte());
    }
    html.execute("http://xxx:8080/jsp/main.jsp",30,30);
    if ( html.getByte() != null && html.getByte().length > 0 ){
     Utils.fileWriteByte("/sdcard/Download/test2.html",html.getByte());
    }
     } catch (Throwable ex) {
      ex.printStackTrace();
     }
    }
   });
  nthread.start();
 }


안드로이드에서 호출할때는 main ui thread에서 호출하면 오류가 발생하기 때문에 thread를 사용하였습니다.
그리고 권한 <uses-permission android:name="android.permission.INTERNET" /> 을 추가해주어야 합니다.

추가내용
http://swlock.blogspot.kr/2016/07/http-session-html.html
위에서 쿠키 작업한 내용이 완벽하지 않습니다. 
https://en.wikipedia.org/wiki/HTTP_cookie 
https://tools.ietf.org/html/rfc2109
여기 보면 자세한 내용이 나오는데 안의 인자값에 따라 어떤 동작들을 해야합니다. 그러한 부분이 전혀 구현되어있지 않습니다.
따라서 완벽하게 하려면 httpclient libarary등의 라이브러리를 사용하시기 바랍니다.
http://swlock.blogspot.kr/2017/01/httpclient-use-in-android-httpclient.html

번호 제목 글쓴이 날짜 조회 수
128 단말기 고유값 구하는 방법들 황제낙엽 2019.03.03 11739
127 뷰 캡처하여 이미지 파일로 저장하기(화면 캡처)-04 file 황제낙엽 2018.08.12 1711
126 고유 식별자의 모범 사례 (Android Developers) 황제낙엽 2019.03.03 1106
125 Emulator: audio: Failed to create voice `adc' 황제낙엽 2018.08.06 1007
124 뷰 캡처하여 이미지 파일로 저장하기(화면 캡처)-06 file 황제낙엽 2018.08.19 991
123 HTTP 프로토콜을 이용한 Json Post 보내고 받기 file 황제낙엽 2017.08.03 816
122 Error:Execution failed for task ':app:lintVitalRelease'. 황제낙엽 2018.01.29 783
121 파일 입출력(내장 메모리, 외장메모리) 황제낙엽 2018.08.19 720
» HttpURLConnection 에서 세션 유지하기(쿠키 사용) 황제낙엽 2017.08.03 661
119 [성공샘플] HttpURLConnection 을 이용하여 JSON 데이터 보내기 예제 황제낙엽 2018.11.10 649
118 Image to byte Array (바로 사용가능한 JPEG 파일) 황제낙엽 2018.07.24 612
117 STT 학습 링크 모음 (sample link) 황제낙엽 2018.10.11 552
116 ABI 관리 황제낙엽 2017.03.28 535
115 TTS 를 위한 스마트폰 설정 및 TTS 샘플 file 황제낙엽 2019.02.16 460
114 안드로이드 스튜디오(Android Studio) 최적화 file 황제낙엽 2018.02.07 433
113 android.webkit.CookieManager 를 이용한 웹뷰와의 세션 공유 황제낙엽 2019.04.26 322
112 동적 레이아웃 생성과 자동 줄바꿈 구현 file 황제낙엽 2018.12.26 311
111 Android - Actionbar에 tab을 추가하고 스와이프 동작으로 화면 전환 구현( ViewPager와 FragmentPagerAdapter 사용) file 황제낙엽 2017.09.11 308
110 안드로이드 두지점(위도,경도) 사이의 거리 file 황제낙엽 2017.01.25 257
109 안드로이드 기기 식별 방법 - UUID(Universally unique identifier) 황제낙엽 2019.03.03 234