package XXX; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.InetSocketAddress; import java.net.MalformedURLException; import java.net.ProtocolException; import java.net.Proxy; import java.net.URL; import java.net.URLConnection; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.HashMap; import java.util.Map; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.apache.log4j.Logger; import sun.misc.BASE64Encoder; public class HttpUtil { private static Logger logger = Logger.getLogger(HttpUtil.class); private static String enc = "UTF-8"; public String post(String urlStr, Map<String, String> paramMap){ return post(urlStr, paramMap, null); } public static String post(String urlStr, Map<String, String> paramMap, Map<String, Object> proxyMap){ Map<String, String> formatUrlMap = formatUrlStr(urlStr); if(formatUrlMap==null){ return ""; } urlStr = formatUrlMap.get("url"); logger.info("url请求地址:"+urlStr); String urlParam = formatUrlMap.get("param"); String allParam = formatParam(urlParam, paramMap); logger.info("整合得到参数串:"+allParam); URL url = null; URLConnection urlConnection = null; try { url = new URL(urlStr); if(proxyMap!=null&&proxyMap.size()>0&&proxyMap.get("proxy")!=null){ Proxy proxy = (Proxy)proxyMap.get("proxy"); urlConnection = url.openConnection(proxy); logger.info("使用代理模式连接"); }else{ urlConnection = url.openConnection(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if(urlConnection==null){ logger.info("创建url连接失败"); return ""; } if(urlStr.startsWith("https")){ logger.info("请求地址是https类型,进行设置"); SSLSocketFactory sslSocketFactory = null; try { SSLContext sslContext = SSLContext.getInstance("SSL"); TrustManager[] trustManagers = initTrustManager(); sslContext.init(null, trustManagers, new SecureRandom()); sslSocketFactory = sslContext.getSocketFactory(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyManagementException e) { e.printStackTrace(); } HttpsURLConnection httpsURLConnection = (HttpsURLConnection)urlConnection; httpsURLConnection.setSSLSocketFactory(sslSocketFactory); httpsURLConnection.setHostnameVerifier(initHostnameVerifier()); } HttpURLConnection httpURLConnection = (HttpURLConnection)urlConnection; //如果有代理,查看是否有账号密码授权,进行授权 if(proxyMap!=null&&proxyMap.size()>0&&proxyMap.get("proxy")!=null){ Object accountObj = proxyMap.get("account"); Object passwordObj = proxyMap.get("password"); String account = ""; if(accountObj!=null){ account = (String)accountObj; } String password = ""; if(passwordObj!=null){ password = (String)passwordObj; } if(!"".equals(account)&&!"".equals(password)){ logger.info("代理授权账号和密码:"+account+" / "+password); String headerKey = "Proxy-Authorization"; BASE64Encoder base64Encoder = new BASE64Encoder(); String headerVal = "Basic "+base64Encoder.encode((account+":"+password).getBytes()); httpURLConnection.setRequestProperty(headerKey, headerVal); } } httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); httpURLConnection.setUseCaches(false); httpURLConnection.setConnectTimeout(5000); httpURLConnection.setReadTimeout(5000); httpURLConnection.setRequestProperty("accept", "*/*"); httpURLConnection.setRequestProperty("connection", "Keep-Alive"); httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.9.1.2) Gecko/20090803 Fedora/3.5.2-2.fc11 Firefox/3.5.2"); //requestMethod默认POST try { httpURLConnection.setRequestMethod("POST"); } catch (ProtocolException e) { e.printStackTrace(); } httpURLConnection.setRequestProperty("Content-type","application/x-www-form-urlencoded;charset="+enc); httpURLConnection.setRequestProperty("Content-Length", allParam.length()+""); BufferedWriter bw = null; OutputStreamWriter osw = null; OutputStream os = null; try { os = httpURLConnection.getOutputStream(); osw = new OutputStreamWriter(os); bw = new BufferedWriter(osw); bw.write(allParam); bw.flush(); httpURLConnection.connect(); } catch (IOException e) { e.printStackTrace(); } finally{ try { if(bw!=null){ bw.close(); } if(osw!=null){ osw.close(); } if(os!=null){ os.close(); } } catch (IOException e) { e.printStackTrace(); } } String result = null; BufferedReader br = null; InputStreamReader isr = null; InputStream is = null; try { int responseCode = httpURLConnection.getResponseCode(); logger.info("返回码:"+responseCode); if(responseCode==200){ is = httpURLConnection.getInputStream(); isr = new InputStreamReader(is); br = new BufferedReader(isr); StringBuilder sb = new StringBuilder(); String temp = null; while((temp=br.readLine())!=null){ sb.append(temp); } result = sb.toString(); } } catch (IOException e) { e.printStackTrace(); } finally { try { if(br!=null){ br.close(); } if(isr!=null){ isr.close(); } if(is!=null){ is.close(); } } catch (IOException e) { e.printStackTrace(); } } return result; } /** * 添加代理 * @param hostname * @param port * @param account * @param password * @return */ public static Map<String, Object> addProxy(String hostname,int port,String account,String password){ Proxy proxy = null; try { InetSocketAddress address = new InetSocketAddress(hostname, port); proxy = new Proxy(Proxy.Type.HTTP, address); } catch (Exception e) { e.printStackTrace(); logger.error("添加代理异常"); } Map<String, Object> map = new HashMap<String, Object>(); map.put("proxy", proxy); map.put("account", account); map.put("password", password); return map; } private static Map<String, String> formatUrlStr(String urlStr){ Map<String, String> map = new HashMap<String, String>(); if(urlStr==null||urlStr.trim().length()==0){ logger.warn("URL为空"); return null; } String param = null; if(urlStr.contains("?")){ int urlEndIndex = urlStr.indexOf("?"); param = urlStr.substring(urlEndIndex+1); urlStr = urlStr.substring(0, urlEndIndex); } if(!urlStr.startsWith("http")){ if(urlStr.startsWith("//")){ urlStr = "http:" + urlStr; }else{ urlStr = "http://" + urlStr; } } map.put("url", urlStr); map.put("param", param); return map; } private static String formatParam(String param,Map<String, String> paramMap){ Map<String, String> m = null; if(paramMap==null){ m = new HashMap<String, String>(); }else{ m = paramMap; } if(param!=null&¶m.length()>0){ String[] keyAndValues = param.split("&"); if(keyAndValues!=null&&keyAndValues.length>0){ for(String keyAndValue :keyAndValues){ String[] arr = keyAndValue.split("=",2); if(arr==null||arr.length==0){ continue; } String key = arr[0]; String value = arr[1]; m.put(key, value); } } } if(m==null||m.size()==0){ return ""; } String rs = ""; for(String key :m.keySet()){ String value = m.get(key); rs += (key+"="+value+"&"); } return rs; } private static TrustManager[] initTrustManager(){ TrustManager X509TrustManager = new X509TrustManager() { @Override public X509Certificate[] getAcceptedIssuers() { return null; } @Override public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { } @Override public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { } }; TrustManager[] trustManagers = {X509TrustManager}; return trustManagers; } private static HostnameVerifier initHostnameVerifier(){ return new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } }; } }