sitelink1 | http://rahxephon.tistory.com/894 |
---|---|
sitelink2 | |
sitelink3 | |
sitelink4 | |
sitelink5 | |
sitelink6 |
이 클래스를 몰랐던 때에는 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(); //2024.03.15 - HttpURLConnection 의 connection 을 끊어버린다
// 호출후에는 응답을 꼭 받아야한다. (2024.03.15 - out.close() 로 인해 connection 이 끊기게 되므로 close를 호출하지 말아야 한다)
con.getResponseCode();
// 결과값을 가져온다.
InputStream in = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[4096];
try
{
in = con.getInputStream(); //2024.03.15 - out.close() 를 실행했다면 위 getOutputStream 에서 사용했던 connection 과는 다른 connection 이 새로 연결된다.
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()가 필요없는 것인지 아니면 다른 경우에 사용되는지는 좀더 찾아봐야겠습니다.
2024.03.15 결과적으로 위에 포스팅했던 학생은 HttpURLConnection 클래스 내에서 connection pool 관리에 대한 전체적인 이해가 부족했다고 판단한다.
그리고 위 코드를 복붙해서 사용했던 나역시 위와 같은 잘못된 코드를 사용하고 있었다.
이번에 Gemini 를 통해서 HttpURLConnection 클래스의 connection pool 의 존재와 API 함수들과의 연결 특성들을 알게 되면서 위 코드의 문제점을 알게 되었다.