상세 컨텐츠

본문 제목

HttpURLConnection 사용하기

프로그래밍/JAVA

by 라제폰 2009. 3. 6. 20:08

본문

이 클래스를 몰랐던 때에는 get 이나 post 로 URL 을 호출하려면 소켓을 열어 헤더 및 콘텐츠 부분을 사이즈까지 모두 계산해서 넣어주었었는데, HttpURLConnection 클래스를 사용하면 별도의 소켓사용없이 URL 을 호출할 수 있다.  늦게나마 알았으니 다행이다.

URL url = new URL("주소");
HttpURLConnection con = (HttpURLConnection)url.openConnection();

// 서버로부터 메세지를 받을 수 있도록 한다. 기본값은 true이다.
con.setDoInput(true);

// 헤더값을 설정한다.
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

// 전달 방식을 설정한다. POST or GET, 기본값은 GET 이다.
con.setRequestMethod(method);

// 서버로 데이터를 전송할 수 있도록 한다. GET방식이면 사용될 일이 없으나, true로 설정하면 자동으로 POST로 설정된다. 기본값은 false이다.
con.setDoOutput(true);

// POST방식이면 서버에 별도의 파라메터값을 넘겨주어야 한다.
String paramstr = buildParameters(params);
OutputStream out = con.getOutputStream();
out.write( paramstr.getBytes("UTF-8") );
out.flush();
out.close();

// con.getResponseCode();

// 결과값을 가져온다.
InputStream in = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[4096];
try
{
    in = con.getInputStream();
    while(true)
    {
        int readlen = in.read(buf);
        if( readlen < 1 )
            break;
        bos.write(buf, 0, readlen);
    }
    String output = new String(bos.toByteArray(), "UTF-8");
    System.out.println(output);
}catch (Exception e){
    e.printStackTrace();
}finally{
    if(bos != null) try{bos.close();}catch(IOException aa){}
    if(in != null) try{bos.close();}catch(IOException aa){}
}

호출후에는 응답을 꼭 받아야한다.
con.getResponseCode();
그렇지 않으면 연결자체를 하지 않는다.

in = con.getInputStream();
in.close();
이렇게 InputStream을 가져와도 호출이 가능하다.

즉, 요청후에는 getResponseCode()를 호출하거나 InputStream()을 얻어야 정상적으로 연결이 이루어진다.

아래는 POST로 파라메터값을 보낼때 유용한 메소드입니다.
rath님의 me2day라이브러리를 참조했습니다.

private String buildParameters( Map params )  throws IOException
{
    StringBuilder sb = new StringBuilder();
    for(Iterator i= params.keySet().iterator(); i.hasNext(); )
    {
        String key = (String)i.next();
        sb.append( key );
        sb.append( "=" );
        sb.append( URLEncoder.encode((String)params.get(key), "UTF-8") );
        if( i.hasNext() )
        sb.append( "&" );
    }
    return sb.toString();
}

HttpURLConnection에 보면 connect()과 disconnect()메소드가 있습니다.
위의 예에서 보면 connect()를 호출하지 않아도 정상적으로 연결이 이루어졌기 때문에
disconnect()를 호출하지 않았습니다.

connect()가 필요없는 것인지 아니면  다른 경우에 사용되는지는 좀더 찾아봐야겠습니다.


관련글 더보기