private static final String TAG = "HttpManager";
private static final int CONNECT_TIME_OUT = 5000;
public static String doHttpPost(String sUrl, String xml) {
if (TextUtils.isEmpty(sUrl)) {
Log.e(TAG, "http post url is empty!");
return "";
}
if (sUrl.indexOf("https") > -1) {
Log.e(TAG, "https post!");
return "";
}
HttpURLConnection conn = null;
try {
URL url = new URL(sUrl);
conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(CONNECT_TIME_OUT);
conn.setRequestMethod("POST");// 必须大写
conn.setDoOutput(true);
conn.setChunkedStreamingMode(0);
writeOutputStream(conn.getOutputStream(), xml);
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
return readInputStream(conn.getInputStream());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
conn = null;
}
}
return "";
}
public static String doHttpsPost(String sUrl, String xml) {
if (TextUtils.isEmpty(sUrl)) {
Log.e(TAG, "https post url is empty!");
return "";
}
if (sUrl.indexOf("https") > -1) {
trustEveryone();
HttpsURLConnection conn = null;
try {
URL url = new URL(sUrl);
conn = (HttpsURLConnection)url.openConnection();
conn.setConnectTimeout(CONNECT_TIME_OUT);
conn.setRequestMethod("POST");// 必须大写
conn.setDoOutput(true);
conn.setChunkedStreamingMode(0);
writeOutputStream(conn.getOutputStream(), xml);
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
return readInputStream(conn.getInputStream());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
conn = null;
}
}
}
return "";
}
private static void writeOutputStream(OutputStream out, String outParams) {
OutputStreamWriter writer = null;
try {
writer = new OutputStreamWriter(out, "UTF-8");
writer.write(outParams);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static String readInputStream(InputStream in) {
StringBuilder builder = new StringBuilder();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(in));
String buffer = "";
while ((buffer = reader.readLine()) != null) {
builder.append(buffer);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
reader = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
return builder.toString();
}
private static void trustEveryone() {
try {
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, new X509TrustManager[] {
new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}
}, null);
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
} catch (Exception e) {
e.printStackTrace();
}
}