String result = null; URL url = null; HttpURLConnection connection = null; InputStreamReader in = null; try { String paramsEncoded = ""; if (params != null) { paramsEncoded = urlParamsFormat(params, "UTF-8"); } url = new URL(urlStr); if (url.getProtocol().toUpperCase().equals("HTTPS")) { trustAllHost(); HttpsURLConnection https = (HttpsURLConnection) url.openConnection(); connection = https; } else { connection = (HttpURLConnection) url.openConnection(); } connection.setDoInput(true); connection.setDoOutput(true); connection.setConnectTimeout(5000); connection.setReadTimeout(20000); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("Charset", "utf-8"); DataOutputStream dop = new DataOutputStream( connection.getOutputStream()); dop.writeBytes(paramsEncoded); dop.flush(); dop.close(); if (connection.getResponseCode() == connection.HTTP_OK) { in = new InputStreamReader(connection.getInputStream()); BufferedReader bufferedReader = new BufferedReader(in); StringBuffer strBuffer = new StringBuffer(); String line = null; while ((line = bufferedReader.readLine()) != null) { strBuffer.append(line); } result = strBuffer.toString(); } else { } } catch (Exception e) { e.printStackTrace(); } finally { if (connection != null) { connection.disconnect(); } if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; }
/** * Returns a String that is suitable for use as an application/x-www-form-urlencoded * list of parameters in an HTTP PUT or HTTP POST. * * @param params * @param charset * @return * @throws UnsupportedEncodingException */ public static String urlParamsFormat(Map<String, String> params, String charset) throws UnsupportedEncodingException { final StringBuilder sb = new StringBuilder(); for (String key : params.keySet()) { final String val = params.get(key); if (!TextUtils.isEmpty(key) && !TextUtils.isEmpty(val)) { final String encodedName = URLEncoder.encode(key, charset); final String encodedValue = URLEncoder.encode(val, charset); if (sb.length() > 0) { sb.append(PARAMETER_SEPARATOR); } sb.append(encodedName).append(NAME_VALUE_SEPARATOR) .append(encodedValue); } }
return sb.toString(); }
/** * trust all host */ private static void trustAllHost() { // Create a trust manager that does not validate certificate chains // Android use X509 cert TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[]{}; } public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } }}; // Install the all-trusting trust manager try { SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, trustAllCerts, new SecureRandom()); HttpsURLConnection .setDefaultSSLSocketFactory(sc.getSocketFactory()); } catch (Exception e) { e.printStackTrace(); } } }