`
lhg4study
  • 浏览: 10937 次
文章分类
社区版块
存档分类
最新评论

Java Http请求处理

 
阅读更多
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.Map.Entry;

/**
 * 
 * HTTP请求处理类
 * 
 * 
 */

public class HttpUtils {

	public static final int GET = 1;
	public static final int POST = 2;
	/**
	 * 默认的编码方式
	 */
	private static final String DEFAULT_CHARSET = "utf-8";
	/**
	 * 默认的connect timeout 30s
	 */
	private static final int DEFAULT_CONNECT_TIMEOUT = 30000;
	/**
	 * 默认的read timeout 30s
	 */
	private static final int DEFAULT_READ_TIMEOUT = 30000;

	/**
	 * GET请求
	 * 
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public static String doGet(String url) throws Exception {
		return service(url, GET, null, DEFAULT_CONNECT_TIMEOUT,
				DEFAULT_READ_TIMEOUT, null, true, DEFAULT_CHARSET);
	}

	/**
	 * GET请求
	 * 
	 * @param url
	 * @param timeout
	 * @return
	 * @throws Exception
	 */
	public static String doGet(String url, int timeout)
			throws Exception {
		return service(url, GET, null, timeout, timeout, null, true,
				DEFAULT_CHARSET);
	}

	/**
	 * GET请求
	 * 
	 * @param url
	 * @param params
	 * @param timeout
	 * @return
	 * @throws Exception
	 */
	public static String doGet(String url, Map<String, String> params,
			int timeout) throws Exception {
		return service(url, GET, null, timeout, timeout, params, true,
				DEFAULT_CHARSET);
	}

	/**
	 * POST请求
	 * 
	 * @param url
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String doPost(String url, Map<String, Object> params)
			throws Exception {
		return service(url, POST, params, DEFAULT_CONNECT_TIMEOUT,
				DEFAULT_READ_TIMEOUT, null, true, DEFAULT_CHARSET);
	}

	/**
	 * POST请求
	 * 
	 * @param url
	 * @param params
	 * @param timeout
	 * @return
	 * @throws Exception
	 */
	public static String doPost(String url, Map<String, Object> params,
			int timeout) throws Exception {
		return service(url, POST, params, timeout, timeout, null, true,
				DEFAULT_CHARSET);
	}

	/**
	 * POST请求
	 * 
	 * @param url
	 * @param params
	 * @param requestProps
	 * @param useCaches
	 * @return
	 * @throws Exception
	 */
	public static String doPost(String url, Map<String, Object> params,
			Map<String, String> requestProps, int timeout, boolean useCaches)
			throws Exception {
		return service(url, POST, params, DEFAULT_CONNECT_TIMEOUT,
				DEFAULT_READ_TIMEOUT, requestProps, useCaches, DEFAULT_CHARSET);
	}

	/**
	 * 发送http请求
	 * 
	 * @param url
	 * @param type
	 * @param params
	 * @param charset
	 * @return
	 * @throws Exception
	 */
	public static String service(String url, int type,
			Map<String, Object> params, int conTimeout, int readTimeout,
			Map<String, String> requestProps, boolean useCaches, String charset)
			throws Exception {

		String response = null;
		HttpURLConnection con = null;
		try {
			URL urls = new URL(url);
			con = (HttpURLConnection) urls.openConnection();
			if (requestProps != null) {
				for (Entry<String, String> entry : requestProps.entrySet()) {
					con.setRequestProperty(entry.getKey(), entry.getValue());
				}
			}
			con.setUseCaches(useCaches);
			con.setDoInput(true);
			con.setDoOutput(true);
			con.setConnectTimeout(conTimeout);
			con.setReadTimeout(readTimeout);
			con.setRequestMethod(type == GET ? "GET" : "POST");

			if (params != null && params.size() > 0) {
				OutputStream writer = null;
				try {
					writer = con.getOutputStream();
					for (Entry<String, Object> entry : params.entrySet()) {
						String x = entry.getKey() + "=" + entry.getValue()
								+ "&";
						writer.write(x.getBytes());
					}
				} finally {
					if (writer != null) {
						writer.close();
					}
				}
			}

			int code = con.getResponseCode();

			if (code == 200) {
				BufferedReader reader = null;
				try {
					reader = new BufferedReader(new InputStreamReader(
							con.getInputStream(), charset));
					StringBuffer buf = new StringBuffer();
					int count = -1;
					char[] tmp = new char[4096];
					while ((count = reader.read(tmp)) != -1) {
						buf.append(new String(tmp, 0, count));
					}
					response = buf.toString();

				} finally {
					if (reader != null) {
						reader.close();
					}
				}
			} else {
				handeException(String.valueOf(code), url);
			}

		} catch (Exception e) {
			handeException(e.getMessage(), url);

		} finally {
			if (con != null) {
				con.disconnect();
			}
		}

		return response;
	}

	private static void handeException(String tip, String url)
			throws Exception {
		String errorInfo = StringUtils.format(
				"Http request failed(%1$s), url:%2$s", tip, shortenUrl(url));
		throw new Exception(errorInfo);
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics