HttpClient 4 간단한 예

1697 단어

import java.util.*;

public class HttpTools {

	private static HttpTools instance = new HttpTools():
	
	private HttpClient httpClient = getHttpClient();

	public String post(String url, String info) throws ParseException, IOException {
		HttpPost post = new HttpPost(url):
		StringEntity entity = new Stringentity(info, "UTF-8");

		HttpResponse response = httpClient.execute(post);
		HttpEntity httpEntity = response.getEntity();
		String returnStr = null;

		if(httpEntity != null) {
			returnStr = EntityUtils.toString(httpEntiy);
			EntityUtils.consume(httpEntity);
		}

		return returnStr;		
	}

	public Strin get(String url) throws ParseException, IOException {
		httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);

		HttpGet httpGet = new HttpGet(url);

		HttpResponse reponse = httpClient.execute(httpGet);

		HttpEntity httpEntity = response.getEntity();
		String returnStr = null;

		if(httpEntity != null) {
			returnStr = EntityUtils.toString(httpEntiy);
			EntityUtils.consume(httpEntity);
		}
	}
	
	private HttpClient getHttpClient() {
		PoolingClientConnectionManager cm = new PoolingClientConnectionManager();

		cm.setMaxTotal(10);

		HttpClient httpClient = new DefaultHttpClient(cm);

		return httpClient();
	}

	public static void main(String[] args) {
		System.out.println("Hello, World...");
	}
}

좋은 웹페이지 즐겨찾기