| | |
| | | public class HttpUtils { |
| | | |
| | | private static final String TAG = "HttpUtils"; |
| | | private static final int mReadTimeOut = 1000 * 10; // 10秒 |
| | | private static final int mConnectTimeOut = 1000 * 5; // 5秒 |
| | | |
| | | /** |
| | | * 10秒 |
| | | */ |
| | | private static final int READ_TIME_OUT = 1000 * 10; |
| | | |
| | | /** |
| | | * 5秒 |
| | | */ |
| | | private static final int CONNECT_TIME_OUT = 1000 * 5; |
| | | |
| | | private static final String CHAR_SET = "UTF-8"; |
| | | private static final int mRetry = 2; // 默认尝试访问次数 |
| | | |
| | | /** |
| | | * 默认尝试访问次数 |
| | | */ |
| | | private static final int RETRY = 2; |
| | | |
| | | private static String mQ1="?"; |
| | | |
| | | private static char mQ2='?'; |
| | | |
| | | private static char mAnd='&'; |
| | | |
| | | private static String mHttps="https"; |
| | | |
| | | public static String get(String url) throws Exception { |
| | | return get(url, null); |
| | |
| | | } |
| | | |
| | | if (params != null && params.size() > 0) { |
| | | if (!url.contains("?")) { |
| | | if (!url.contains(mQ1)) { |
| | | url += "?"; |
| | | } |
| | | |
| | | if (url.charAt(url.length() - 1) != '?') { |
| | | if (url.charAt(url.length() - 1) != mQ2) { |
| | | url += "&"; |
| | | } |
| | | |
| | |
| | | |
| | | StringBuilder builder = new StringBuilder(); |
| | | for (Map.Entry<String, ? extends Object> entry : params.entrySet()) { |
| | | if (entry.getKey() != null && entry.getValue() != null) |
| | | builder.append(entry.getKey().trim()).append("=") |
| | | .append(URLEncoder.encode(entry.getValue().toString(), CHAR_SET)).append("&"); |
| | | if (entry.getKey() != null && entry.getValue() != null) { |
| | | builder.append(entry.getKey().trim()).append("=").append(URLEncoder.encode(entry.getValue().toString(), CHAR_SET)).append("&"); |
| | | } |
| | | } |
| | | |
| | | if (builder.charAt(builder.length() - 1) == '&') { |
| | | if (builder.charAt(builder.length() - 1) == mAnd) { |
| | | builder.deleteCharAt(builder.length() - 1); |
| | | } |
| | | |
| | |
| | | private static String tryToGet(String url, Map<String, String> headers) throws Exception { |
| | | int tryTime = 0; |
| | | Exception ex = null; |
| | | while (tryTime < mRetry) { |
| | | while (tryTime < RETRY) { |
| | | try { |
| | | return doGet(url, headers); |
| | | } catch (Exception e) { |
| | | if (e != null) |
| | | if (e != null) { |
| | | ex = e; |
| | | } |
| | | tryTime++; |
| | | } |
| | | } |
| | | if (ex != null) |
| | | if (ex != null) { |
| | | throw ex; |
| | | else |
| | | } |
| | | else { |
| | | throw new Exception("未知网络错误 "); |
| | | } |
| | | } |
| | | |
| | | private static String doGet(String strUrl, Map<String, String> headers) throws Exception { |
| | |
| | | private static String tryToPost(String url, String postContent, Map<String, String> headers) throws Exception { |
| | | int tryTime = 0; |
| | | Exception ex = null; |
| | | while (tryTime < mRetry) { |
| | | while (tryTime < RETRY) { |
| | | try { |
| | | return doPost(url, postContent, headers); |
| | | } catch (Exception e) { |
| | | if (e != null) |
| | | if (e != null) { |
| | | ex = e; |
| | | } |
| | | tryTime++; |
| | | } |
| | | } |
| | | if (ex != null) |
| | | if (ex != null) { |
| | | throw ex; |
| | | else |
| | | } |
| | | else { |
| | | throw new Exception("未知网络错误 "); |
| | | } |
| | | } |
| | | |
| | | private static String doPost(String strUrl, String postContent, Map<String, String> headers) throws Exception { |
| | |
| | | } |
| | | |
| | | private static void configConnection(HttpURLConnection connection) { |
| | | if (connection == null) |
| | | if (connection == null) { |
| | | return; |
| | | connection.setReadTimeout(mReadTimeOut); |
| | | connection.setConnectTimeout(mConnectTimeOut); |
| | | } |
| | | connection.setReadTimeout(READ_TIME_OUT); |
| | | connection.setConnectTimeout(CONNECT_TIME_OUT); |
| | | |
| | | connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); |
| | | connection.setRequestProperty("User-Agent", |
| | |
| | | if (strUrl == null) { |
| | | return null; |
| | | } |
| | | if (strUrl.toLowerCase().startsWith("https")) { |
| | | if (strUrl.toLowerCase().startsWith(mHttps)) { |
| | | return getHttpsConnection(strUrl); |
| | | } else { |
| | | return getHttpConnection(strUrl); |
| | |
| | | } |
| | | |
| | | private static X509TrustManager xtm = new X509TrustManager() { |
| | | @Override |
| | | public void checkClientTrusted(X509Certificate[] chain, String authType) { |
| | | } |
| | | |
| | | @Override |
| | | public void checkServerTrusted(X509Certificate[] chain, String authType) { |
| | | } |
| | | |
| | | @Override |
| | | public X509Certificate[] getAcceptedIssuers() { |
| | | return null; |
| | | } |
| | | }; |
| | | |
| | | private static HostnameVerifier hnv = new HostnameVerifier() { |
| | | @Override |
| | | public boolean verify(String hostname, SSLSession session) { |
| | | return true; |
| | | } |