티스토리 뷰

프로그래밍/Java

HTTP Post 요청

메모하는습관 2010. 8. 13. 14:41
http://hc.apache.org/httpclient-3.x/index.html

위 사이트에서 관련 라이브러리를 다운 받는다.

아래는 예제 코드 이다.
HttpClient client = null;
PostMethod post = null;
try
{
	String url = "http://localhost/test.jsp";
	client = new HttpClient(new MultiThreadedHttpConnectionManager());
	post = new PostMethod(url);

	// timeout 설정
	HttpMethodParams hmp = new HttpMethodParams();
	hmp.setSoTimeout(1000*3);
	post.setParams(hmp);

	// parameters
	NameValuePair[] param = {
		new NameValuePair("param1", "1"),
		new NameValuePair("param2", "2"),
		new NameValuePair("param3", "3")
	};
	post.setRequestBody(param);
	int httpResultCode = client.executeMethod(post);

	if(httpResultCode == HttpStatus.SC_OK)
	{
		InputStream in = null;
		try
		{
			in = post.getResponseBodyAsStream();
			content = inputStreamToString(in);
		}
		catch(Exception e)
		{
			// 오류 처리
		}
		finally
		{
			// inputstream close
			if(in != null)
				in.close();
		}
	}
	else
	{
		// 요청 실패 처리
	}
}
catch(Exception e)
{
	// post 요청 오류 처리
}
finally
{
	// post close
	if(post != null)
		post.releaseConnection();
}




댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday