Java 使用 HttpClient 发送 Post请求(http与https)
发送 http 请求
**发送 http 请求 **
package com.zealotpz.demo;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class HttpClientTest {
//设置链接超时和请求超时等参数,否则会长期停止或者崩溃
private static RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).setConnectionRequestTimeout(60000).build();
public static String sendPost(String url, Map<String, String> params) {
String responseContent = null;
try {
//建立HttpPost对象
HttpPost httpPost = new HttpPost(url);
//建立一个NameValuePair数组,用于存储欲传送的参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
//遍历参数 map,添加参数
for (String key : params.keySet()) {
nvps.add(new BasicNameValuePair(key, params.get(key)));
}
//参数集合传入到一个UrlEncodedFormEntity中并设置编码
httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
//发送Post,并返回一个HttpResponse对象
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
//使用响应对象获取响应实
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
//将响应实体转为字符串
responseContent = EntityUtils.toString(httpEntity, "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
}
//返回应答字符串
return responseContent;
}
发送 https 请求
**发送 https 请求 **
package com.zealotpz.demo;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
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.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Created by zealotpz on 2017/2/9.
*/
public class HttpClientTest {
//设置链接超时和请求超时等参数,否则会长期停止或者崩溃
private static RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).setConnectionRequestTimeout(60000).build();
public static String sendHttpsPost(String url, Map<String, String> params) {
String responseContent = null;
try {
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
for (String key : params.keySet()) {
nvps.add(new BasicNameValuePair(key, params.get(key)));
}
httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
httpPost.addHeader("Content-Type", "application/json; charset=UTF-8");//修改请求头
// httpPost.setEntity(new StringEntity(JacksonUtils.toJson(encrypt),"UTF-8"));//传递json数据对象encrypt为自己创建的Dto对象
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()).setDefaultRequestConfig(requestConfig).build();
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
responseContent = EntityUtils.toString(httpEntity, HTTP.UTF_8);
}
} catch (Exception e) {
e.printStackTrace();
}
return responseContent;
}
创建SSL安全连接
//创建SSL安全连接
private static SSLConnectionSocketFactory createSSLConnSocketFactory() {
SSLConnectionSocketFactory sslsf = null;
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
sslsf = new SSLConnectionSocketFactory(sslContext, new X509HostnameVerifier() {
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
public void verify(String host, SSLSocket ssl) throws IOException {
}
public void verify(String host, X509Certificate cert) throws SSLException {
}
public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException {
}
});
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
return sslsf;
}
}
public static void main(String[] args) {
String url ="https://rsb.pingan.com.cn/brop/ma/cust/app/market/loan/applyNewLoanC.do";
Encrypt encrypt = new Encrypt();
encrypt.setEncryptFlag("YES");
encrypt.setEncryptParam("HNuto1vtwEer3FaMNHRclszJUP3sT61%2BM62YN8b87S5ha3iiAb%2BboUKTjVaPrCREDrwVnWZERYe0SEFdhboM9DVRHnG3HJxZzByZGALNJxSs5nkAvigv6ErlqdH%2FBcpxHQINl4aXRGVm5AhtEXc5K%2Bu423ULxij%2FG%2BdBRQds5us%3D");
encrypt.setHouseLoan("YES");
encrypt.setInsurancePolicy("YES");
encrypt.setOuterSource("xyd0001");
encrypt.setProvidentFund("YES");
encrypt.setSource("sa0001459");
encrypt.setUserAge("23-55岁");
encrypt.setUserCreditCard("YES");
encrypt.setCity("广东-深圳");
encrypt.setCityCode("440300");
方式一:
/* String url ="http://10.3.110.138:8089/crm/user/login";
paramsMap.put("username","wwl");
paramsMap.put("password","123456");*/
方式二:
String Result =HttpRequestUtils.sendHttpsPost(url,encrypt);
System.out.println(Result);
}
还没有评论,来说两句吧...