HttpClient의 심플한 사용



/**
* Http경로를 인자로 넘겨 서버의 출력결과를 스트링으로 리턴받는다.
*
* @param path http경로
* @return 서버결과
*/
public static String getHttpResult(String path) {

String strXML = null;

HttpClient client = new HttpClient();
GetMethod method = new GetMethod(path);

try {
int returnCode = client.executeMethod(method);

if (returnCode == 200) {
strXML = method.getResponseBodyAsString();
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
method.releaseConnection();
}  

return strXML;
}


위와 같은 static 메서드 하나 만들어놓고, Http로 접근하여 결과를 XML등으로 받아올때 사용하면 편리할 것 같다. 현재는 HttpUtil 이라는 클래스 하나 생성해서 사용하고 있는 중...
아래는 라이브러리에 대한 샘플코드 관련 페이지이다.
http://foldurl.com/207840


그리고 HttpClient 관련된 라이브러리는 아래의 링크에서 다운로드 받아 사용하자
http://commons.apache.org/codec/download_codec.cgi