/**
* post 请求 转json
* Https
* @param url
* @param json
* @return
*/
public static String doPostJson(String url, String json) {
// 创建Httpclient对象
HttpClient httpClient = null;
String resultString = "";
try {
httpClient = new SSLClient();
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 创建请求内容
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
// 请求头
httpPost.addHeader("x-tif-paasid", "tywg");
httpPost.addHeader("x-tif-timestamp", null);
httpPost.addHeader("x-tif-signature", null);
httpPost.addHeader("x-tif-nonce", null);
httpPost.addHeader("Cache-Control", "no-cache");
// 执行http请求
HttpResponse response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
return resultString;
}
/**
* 发送get请求
* Https
* @param url 链接地址
* @param charset 字符编码,若为null则默认utf-8
* @return
*/
public static String doGet(String url, String charset){
if(null == charset){
charset = "utf-8";
}
HttpClient httpClient = null;
HttpGet httpGet= null;
String result = null;
try {
httpClient = new SSLClient();
httpGet = new HttpGet(url);
HttpResponse response = httpClient.execute(httpGet);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,charset);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 发送get请求
*Http
* @return
*/
public static String doGets(String stoptime, String audittype, String ip, Integer istrust, Integer ipmacbind) {
String url = "";
String data = "";
String result = "";
StringBuffer sb = new StringBuffer(url);
if (null != stoptime && !"".equals(stoptime)) {
sb.append("&stoptime=" + stoptime);
}
HttpGet httpGet = new HttpGet(sb.toString());
// 设置参数
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 5000);
HttpConnectionParams.setSoTimeout(params, 5000);
// 执行请求的对象
HttpClient client = new DefaultHttpClient(params);
try {
// 执行请求的对象
HttpResponse resp = client.execute(httpGet);
if (resp.getStatusLine().getStatusCode() == 200) {
// 获取响应 的数据
HttpEntity entity = resp.getEntity();
data = EntityUtils.toString(entity, "utf-8");
JSONObject code = JSONObject.parseObject(data);
result = code.getString("status");
}
} catch (Exception e) {
System.out.println("认证操失败" + e.getMessage());
}
return result;
}
/**
* from-data 请求
* http
* @param url
* @param map
* @return
* @throws Exception
*/
public static String doPostForm(String url, Map map) throws Exception {
String result = "";
String resultString = "";
CloseableHttpClient client = null;
CloseableHttpResponse response = null;
RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(550000).setConnectTimeout(550000)
.setConnectionRequestTimeout(550000).setStaleConnectionCheckEnabled(true).build();
client = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
// client = HttpClients.createDefault();
URIBuilder uriBuilder = new URIBuilder(url);
HttpPost httpPost = new HttpPost(uriBuilder.build());
httpPost.setHeader("Connection", "Keep-Alive");
httpPost.setHeader("Charset", "utf-8");
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
List<NameValuePair> params = new ArrayList<NameValuePair>();
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue());
params.add(pair);
}
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
try {
response = client.execute(httpPost);
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity, "utf-8");
System.out.println("单点登录返回结果=========="+result);
JSONObject content = JSONObject.parseObject(result);
resultString = content.getString("content");
}
}
} catch (ClientProtocolException e) {
throw new RuntimeException("创建连接失败" + e);
} catch (IOException e) {
throw new RuntimeException("创建连接失败" + e);
}
return resultString;
}
/**
* post 请求 转json
* Http
* @param url
* @param json
* @return
*/
public static String doPostJsons(String url, String json) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
String result = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 创建请求内容
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
JSONObject content = JSONObject.parseObject(resultString);
result = content.getString("content");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result;
}
/**
* 用于进行Https请求的HttpClient
* @ClassName: SSLClient
* @author Devin <xxx>
*
*/
public class SSLClient extends DefaultHttpClient {
public SSLClient() throws Exception{
super();
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[]{tm}, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = this.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", 443, ssf));
}
}