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 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 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 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 params,
			Map 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 params, int conTimeout, int readTimeout,
			Map 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 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 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);
	}
}

 

你可能感兴趣的:(java)