Java tutorial
/** * * Licensed Property to China UnionPay Co., Ltd. * * (C) Copyright of China UnionPay Co., Ltd. 2010 * All Rights Reserved. * * * Modification History: * ============================================================================= * Author Date Description * ------------ ---------- --------------------------------------------------- * xshu 2014-05-28 HTTP * ============================================================================= */ package com.zhonghui.tool.controller; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.ProtocolException; import java.net.URISyntaxException; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import java.util.Map; import java.util.Map.Entry; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class HttpClient { private static final Logger logger = LoggerFactory.getLogger(HttpClient.class); /** * ? */ private URL url; /** * */ private int connectionTimeout; /** * */ private int readTimeOut; /** * */ private String result; /** * ? * @return */ public String getResult() { return result; } /** * * @param result */ public void setResult(String result) { this.result = result; } /** * <br> * httpcgt_sdk.properties? * @param url ? */ public HttpClient(String url) { try { this.url = new URL(url); SDKConfig config = SDKConfig.getConfig(); this.connectionTimeout = config.getConnectionTimeout(); this.readTimeOut = config.getReadTimeout(); } catch (MalformedURLException e) { e.printStackTrace(); } } /** * * @param url ? * @param connectionTimeout HTTP * @param readTimeOut HTTP */ public HttpClient(String url, int connectionTimeout, int readTimeOut) { try { this.url = new URL(url); this.connectionTimeout = connectionTimeout; this.readTimeOut = readTimeOut; } catch (MalformedURLException e) { e.printStackTrace(); } } /** * post????,?UTF-8 * @param data ??? * @return * @throws Exception */ public int sendPost(Map<String, String> data) throws Exception { return this.sendPost(data, null); } /** * post???? * @param data * @param encoding * @return * @throws Exception */ public int sendPost(Map<String, String> data, String encoding) throws Exception { try { if (StringUtils.isEmpty(encoding)) { encoding = "UTF-8"; } HttpURLConnection httpURLConnection = createConnection(encoding); if (null == httpURLConnection) { throw new Exception("?"); } String sendData = this.getRequestParamString(data, encoding); logger.info(":[" + sendData + "]"); this.requestServer(httpURLConnection, sendData, encoding); this.result = this.response(httpURLConnection, encoding); logger.info("?:[" + result + "]"); return httpURLConnection.getResponseCode(); } catch (Exception e) { throw e; } } /** * ???? GET? ?utf-8 * @return * @throws Exception */ public int sendGet() throws Exception { return this.sendGet(null); } /** * ???? GET? * @param encoding * @return * @throws Exception */ public int sendGet(String encoding) throws Exception { try { if (StringUtils.isEmpty(encoding)) { encoding = "UTF-8"; } HttpURLConnection httpURLConnection = createConnectionGet(encoding); if (null == httpURLConnection) { throw new Exception("?"); } this.result = this.response(httpURLConnection, encoding); logger.info("?:[" + result + "]"); return httpURLConnection.getResponseCode(); } catch (Exception e) { throw e; } } /** * HTTP Post??? * * @param connection * @param message ??? * @throws IOException */ private void requestServer(final URLConnection connection, String message, String encoder) throws Exception { PrintStream out = null; try { connection.connect(); out = new PrintStream(connection.getOutputStream(), false, encoder); out.print(message); out.flush(); } catch (Exception e) { throw e; } finally { if (null != out) { out.close(); } } } /** * Response? * * @param connection * @param encoding ? * @return * @throws URISyntaxException * @throws IOException */ private String response(final HttpURLConnection connection, String encoding) throws URISyntaxException, IOException, Exception { InputStream in = null; StringBuilder sb = new StringBuilder(1024); BufferedReader br = null; try { if (200 == connection.getResponseCode()) { in = connection.getInputStream(); sb.append(new String(read(in), encoding)); } else { in = connection.getErrorStream(); sb.append(new String(read(in), encoding)); } logger.info("HTTP Return Status-Code:[" + connection.getResponseCode() + "]"); return sb.toString(); } catch (Exception e) { throw e; } finally { if (null != br) { br.close(); } if (null != in) { in.close(); } if (null != connection) { connection.disconnect(); } } } /** * Response? * * @param connection * @param encoding ? * @return * @throws URISyntaxException * @throws IOException */ private InputStream responseInput(final HttpURLConnection connection, String encoding) throws URISyntaxException, IOException, Exception { InputStream in = null; try { if (200 == connection.getResponseCode()) { in = connection.getInputStream(); } else { in = connection.getErrorStream(); } logger.info("HTTP Return Status-Code:[" + connection.getResponseCode() + "]"); return in; } catch (Exception e) { throw e; } finally { if (null != connection) { connection.disconnect(); } } } public static byte[] read(InputStream in) throws IOException { byte[] buf = new byte[1024]; int length = 0; ByteArrayOutputStream bout = new ByteArrayOutputStream(); while ((length = in.read(buf, 0, buf.length)) > 0) { bout.write(buf, 0, length); } bout.flush(); return bout.toByteArray(); } /** * Post * * @return * @throws ProtocolException */ private HttpURLConnection createConnection(String encoding) throws ProtocolException { HttpURLConnection httpURLConnection = null; try { httpURLConnection = (HttpURLConnection) url.openConnection(); } catch (IOException e) { e.printStackTrace(); return null; } httpURLConnection.setConnectTimeout(this.connectionTimeout);// httpURLConnection.setReadTimeout(this.readTimeOut);// ? httpURLConnection.setDoInput(true); // ? post? httpURLConnection.setDoOutput(true); // ? post? httpURLConnection.setUseCaches(false);// ? httpURLConnection.setRequestProperty("Content-type", "application/x-www-form-urlencoded;charset=" + encoding); httpURLConnection.setRequestMethod("POST"); return httpURLConnection; } /** * get * * @return * @throws ProtocolException */ private HttpURLConnection createConnectionGet(String encoding) throws ProtocolException { HttpURLConnection httpURLConnection = null; try { httpURLConnection = (HttpURLConnection) url.openConnection(); } catch (IOException e) { e.printStackTrace(); return null; } httpURLConnection.setConnectTimeout(this.connectionTimeout);// httpURLConnection.setReadTimeout(this.readTimeOut);// ? httpURLConnection.setUseCaches(false);// ? httpURLConnection.setRequestProperty("Content-type", "application/x-www-form-urlencoded;charset=" + encoding); httpURLConnection.setRequestMethod("GET"); return httpURLConnection; } /** * Map?key=value&key=value * * @param requestParam * @param coder * @return */ private String getRequestParamString(Map<String, String> requestParam, String coder) { StringBuffer sf = new StringBuffer(""); String reqstr = ""; if (null != requestParam && 0 != requestParam.size()) { for (Entry<String, String> en : requestParam.entrySet()) { try { sf.append(en.getKey() + "=" + (null == en.getValue() || "".equals(en.getValue()) ? "" : URLEncoder.encode(en.getValue(), coder)) + "&"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return ""; } } reqstr = sf.substring(0, sf.length() - 1); } logger.info("(?URLEncode?):[" + reqstr + "]"); return reqstr; } }