String url = getUfcBeginUrl() + "/interface/test.do";
//添加参数
HashMap parmMap = new HashMap();
parmMap.put("name", name);
parmMap.put("app_key", this.app_key);
//增加签名
parmMap.put("sign", sign);
JSONObject object = SSHttpClient.send(url, parmMap);
return object;
}
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class SSHttpClient {
public static SSLContext SSLClient() throws NoSuchAlgorithmException, KeyManagementException {
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);
return ctx;
}
/**
* 模拟请求
*
* @param url 资源地址
* @param map 参数列表
* @param encoding 编码
* @return
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
* @throws IOException
* @throws ClientProtocolException
*/
public static JSONObject send(String url, Map<String, String> map) {
String body = "", encoding = "UTF-8";
//采用绕过验证的方式处理https请求
SSLContext sslcontext = null;
try {
sslcontext = SSLClient();
} catch (KeyManagementException | NoSuchAlgorithmException e) {
e.printStackTrace();
}
// 设置协议http和https对应的处理socket链接工厂的对象
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", new SSLConnectionSocketFactory(sslcontext)).build();
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
HttpClients.custom().setConnectionManager(connManager);
//创建自定义的httpclient对象
CloseableHttpClient client = HttpClients.custom().setConnectionManager(connManager).build();
// CloseableHttpClient client = HttpClients.createDefault();
//创建post方式请求对象
HttpPost httpPost = new HttpPost(url);
//装填参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
if (map != null) {
for (Entry<String, String> entry : map.entrySet()) {
// nvps.add(new BasicNameValuePair(entry.getKey(), UrlEncoded.encodeString(entry.getValue(), encoding)));
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
//设置参数到请求对象中
try {
httpPost.setEntity(new UrlEncodedFormEntity(nvps, encoding));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println("请求地址:" + url);
System.out.println("请求参数:" + nvps.toString());
//设置header信息
//指定报文头【Content-type】、【User-Agent】
httpPost.setHeader("Content-type", "application/x-www-form-urlencoded;charset=" + encoding);
httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//执行请求操作,并拿到结果(同步阻塞)
CloseableHttpResponse response = null;
try {
response = client.execute(httpPost);
} catch (IOException e) {
e.printStackTrace();
}
//获取结果实体
HttpEntity entity = response.getEntity();
if (entity != null) {
//按指定编码转换结果实体为String类型
try {
body = EntityUtils.toString(entity, encoding);
} catch (ParseException | IOException e) {
e.printStackTrace();
}
}
try {
EntityUtils.consume(entity);
} catch (IOException e) {
e.printStackTrace();
} finally {
//释放链接
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
return JSONObject.fromObject(body);
} catch (Exception e) {
throw new RuntimeException(body);
}
}
/**
* 模拟请求
*
* @param url 资源地址
* @param map 参数列表
* @param encoding 编码
* @return
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
* @throws IOException
* @throws ClientProtocolException
*/
public static CloseableHttpResponse getHttpResponse(String url, Map<String, String> map) {
String body = "", encoding = "UTF-8";
//采用绕过验证的方式处理https请求
SSLContext sslcontext = null;
try {
sslcontext = SSLClient();
} catch (KeyManagementException | NoSuchAlgorithmException e) {
e.printStackTrace();
}
// 设置协议http和https对应的处理socket链接工厂的对象
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", new SSLConnectionSocketFactory(sslcontext)).build();
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
HttpClients.custom().setConnectionManager(connManager);
//创建自定义的httpclient对象
CloseableHttpClient client = HttpClients.custom().setConnectionManager(connManager).build();
// CloseableHttpClient client = HttpClients.createDefault();
//创建post方式请求对象
HttpPost httpPost = new HttpPost(url);
//装填参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
if (map != null) {
for (Entry<String, String> entry : map.entrySet()) {
// nvps.add(new BasicNameValuePair(entry.getKey(), UrlEncoded.encodeString(entry.getValue(), encoding)));
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
//设置参数到请求对象中
try {
httpPost.setEntity(new UrlEncodedFormEntity(nvps, encoding));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println("请求地址:" + url);
System.out.println("请求参数:" + nvps.toString());
//设置header信息
//指定报文头【Content-type】、【User-Agent】
httpPost.setHeader("Content-type", "application/x-www-form-urlencoded;charset=" + encoding);
httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//执行请求操作,并拿到结果(同步阻塞)
try {
return client.execute(httpPost);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}