package com.wisedu.scc.love.utils;
import com.wisedu.scc.love.entity.https.TrustAllSSLSocketFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* HTTPS请求帮助
*/
public class HttpsUtil {
/**
* HttpClient方式实现,支持所有Https免验证方式链接
*/
public static String doHttpsGet(String url, HttpParams httpParams) throws IOException {
int timeOut = 30 * 1000;
HttpParams param = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(param, timeOut);
HttpConnectionParams.setSoTimeout(param, timeOut);
HttpConnectionParams.setTcpNoDelay(param, true);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", TrustAllSSLSocketFactory.getDefault(), 443));
ClientConnectionManager manager = new ThreadSafeClientConnManager(param, registry);
DefaultHttpClient client = new DefaultHttpClient(manager, param);
HttpGet request = new HttpGet(url);
if(null!=httpParams)
request.setParams(httpParams);
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
return result.toString();
}
/**
* HttpClient方式实现,支持所有Https免验证方式链接
*/
public static String doHttpsPost(String url, HttpParams httpParams) throws IOException {
int timeOut = 30 * 1000;
HttpParams param = new BasicHttpParams();
param.setParameter("ean", httpParams.getParameter("ean"));
HttpConnectionParams.setConnectionTimeout(param, timeOut);
HttpConnectionParams.setSoTimeout(param, timeOut);
HttpConnectionParams.setTcpNoDelay(param, true);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", TrustAllSSLSocketFactory.getDefault(), 443));
ClientConnectionManager manager = new ThreadSafeClientConnManager(param, registry);
DefaultHttpClient client = new DefaultHttpClient(manager, param);
HttpPost request = new HttpPost(url);
/*if(null!=httpParams)
request.setParams(httpParams);*/
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
return result.toString();
}
/**
* 对url的地址发送get方式的Http请求
* @return String
*/
public static String getHttpRequest(String url) {
String content = "";
try {
URL getUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
connection.connect();
// 取得输入流,并使用Reader读取
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String lines="";
while ((lines = reader.readLine()) != null) {
content+=lines;
}
reader.close();
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return content;
}
}