HttpUtils 매개 변수 없 이 다른 항목 인터페이스 호출

4000 단어 IDEAJava
 
package com.timespark.util;

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.net.URL;


public class HTTPUtils {

	private final static Logger logger = Logger.getLogger(HTTPUtils.class);

	private final static String OPERATER_NAME = "【HTTP  】";

	private final static int SUCCESS = 200;

	private final static String UTF8 = "UTF-8";

	private HttpClient client;

	private static HTTPUtils instance = new HTTPUtils();

	/**
	 *       
	 */
	private HTTPUtils() {

		HttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
		HttpConnectionManagerParams params = httpConnectionManager.getParams();
		params.setConnectionTimeout(15000);
		params.setSoTimeout(25000);
		params.setDefaultMaxConnectionsPerHost(1000);
		params.setMaxTotalConnections(1000);
		client = new HttpClient(httpConnectionManager);
		client.getParams().setContentCharset(UTF8);
		client.getParams().setHttpElementCharset(UTF8);
	}

	/**
	 * get  
	 */
	public static String get(URL url) {
		return instance.doGet(url,UTF8);
	}
	public static String get(URL url,String codeMa) {
		return instance.doGet(url,codeMa);
	}

	private String doGet(URL url,String codeMa) {
		long beginTime = System.currentTimeMillis();
		String respStr = StringUtils.EMPTY;
		try {
			logger.debug(OPERATER_NAME + "  get  ,  host:" + url);
			HttpMethod method = new GetMethod(url.toString());
			
			//     
			method.getParams().setContentCharset(codeMa);
			try {
				client.executeMethod(method);
			} catch (HttpException e) {

				logger.error(new StringBuffer("  HTTP GET \r
").append(url).append("\r
HTTP \r
"), e); } catch (IOException e) { logger.error(new StringBuffer(" HTTP GET \r
").append(url).append("\r
IO \r
"), e); } if (method.getStatusCode() == SUCCESS) { respStr = method.getResponseBodyAsString(); } // method.releaseConnection(); logger.debug(OPERATER_NAME + " , :" + method.getStatusCode()); logger.debug(OPERATER_NAME + " :" + method.getResponseBodyAsString()); logger.debug(OPERATER_NAME + " .. :" + respStr); } catch (Exception e) { logger.info(OPERATER_NAME, e); } long endTime = System.currentTimeMillis(); logger.debug(OPERATER_NAME + " :" + (endTime - beginTime) + "ms"); return respStr; } /** * POST */ public static String post(URL url ) { return instance.doPost(url); } private String doPost(URL url) { long beginTime = System.currentTimeMillis(); String respStr = StringUtils.EMPTY; try { logger.info(OPERATER_NAME + " post , host:" + url.toString()); PostMethod post = new PostMethod(url.toString()); //RequestEntity requestEntity = new StringRequestEntity(content, "application/json;charse=UTF-8", UTF8); //post.setRequestEntity(requestEntity); // post.getParams().setContentCharset(UTF8); client.executeMethod(post); if (post.getStatusCode() == SUCCESS) { respStr = post.getResponseBodyAsString(); }else{ System.out.println("error: " +post.getResponseBodyAsString()); } logger.info(OPERATER_NAME + " , :" + post.getStatusCode()); logger.info(OPERATER_NAME + " :" + post.getResponseBodyAsString()); logger.info(OPERATER_NAME + " .. :" + respStr); post.releaseConnection(); } catch (Exception e) { logger.error(OPERATER_NAME, e); } long endTime = System.currentTimeMillis(); logger.info(OPERATER_NAME + " :" + (endTime - beginTime) + "ms"); return respStr; } }

좋은 웹페이지 즐겨찾기