java模拟https请求_java-用httpclient模拟发送https请求

该博客介绍了如何在Java中使用HttpClient库来模拟发送HTTPS请求,以避免跨域问题。主要内容包括创建SSLContext,忽略证书校验,以及构建HttpGet和HttpPost请求的方法。
摘要由CSDN通过智能技术生成

--调用项目提供接口,接口是用HTTP URL实现的,最初的想法是另一个项目用jQuery

post进行请求。

---但是,很可能另一个项目是部署在别的机器上,那么就存在跨域问题,而jquery的post请求是不允许跨域的。

---这时,就只能够用HttpClient包进行请求了,同时由于请求的URL是HTTPS的,为了避免需要证书,所以用一-----个类继承DefaultHttpClient类,忽略校验过程。

1.https是需要ssl证书,即使是没用的证书也需要创建.

public static CloseableHttpClient

createSSLClientDefault(){

try {

SSLContext sslContext = new

SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy()

{

//信任所有

public boolean isTrusted(X509Certificate[]

chain,

String authType) throws CertificateException

{

return true;

}

}).build();

SSLConnectionSocketFactory sslsf = new

SSLConnectionSocketFactory(sslContext);

return

HttpClients.custom().setSSLSocketFactory(sslsf).build();

} catch (KeyManagementException e)

{

e.printStackTrace();

} catch (NoSuchAlgorithmExceptione)

{

e.printStackTrace();

} catch (KeyStoreException e) {

e.printStackTrace();

}

return

HttpClients.createDefault();

}

public static String

doPost(String url, String requestXML, String token, String

signatrue, String charset,

boolean pretty, String contentType) {

HttpPost httpPost = new

HttpPost(url);

String resp = null;

try {

if (StringUtils.isNotBlank(token)) {

httpPost.setHeader("4GGOGO-Auth-Token",

token);

}

if (StringUtils.isNotBlank(signatrue))

{

httpPost.setHeader("HTTP-X-4GGOGO-Signature",

signatrue);

}

if (StringUtils.isNotBlank(contentType))

{

httpPost.setHeader("Content-Type",

contentType);

}

httpPost.setEntity(new

StringEntity(requestXML,"utf-8"));

CloseableHttpClient httpClient =

createSSLClientDefault();

HttpResponse httpResponse =

httpClient.execute(httpPost);

HttpEntity entity =

httpResponse.getEntity();

if (entity != null) {

//按指定编码转换结果实体为String类型

resp =

EntityUtils.toString(entity, "UTF-8");

}

} catch

(ClientProtocolException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return resp;

}

public

static String doGet(String url, String queryString, String token,

String signatrue, String charset,

boolean pretty, String contentType) {

StringBuffer response = new

StringBuffer();

HttpClient client = new

HttpClient();

HttpMethod method = new

GetMethod(url);

try {

if (StringUtils.isNotBlank(queryString))

{

method.setQueryString(URIUtil.encodeQuery(queryString));

}

if (StringUtils.isNotBlank(signatrue))

{

method.addRequestHeader("HTTP-X-4GGOGO-Signature",

signatrue);

}

if (StringUtils.isNotBlank(token)) {

method.addRequestHeader("4GGOGO-Auth-Token", token);

}

method.setRequestHeader("content-type",

contentType);

client.executeMethod(method);

System.out.println("返回的状态码为" +

method.getStatusCode());

if (method.getStatusCode() == HttpStatus.SC_OK)

{

BufferedReader reader = new BufferedReader(new

InputStreamReader(method.getResponseBodyAsStream(),

charset));

String

line;

while

((line = reader.readLine()) != null) {

if (pretty) {

response.append(line).append(System.getProperty("line.separator"));

} else {

response.append(line);

}

}

reader.close();

}

} catch (URIException e)

{

System.out.println("执行HTTP Get请求时,编码查询字符串'" +

queryString + "'发生异常!" + e.getMessage());

} catch (IOException e)

{

System.out.println("执行HTTP Get请求时,编码查询字符串'" +

queryString + "'发生异常!" + e.getMessage());

} finally {

method.releaseConnection();

}

return

response.toString();

}

public

static CloseableHttpClient createSSLClientDefault(){

try

{

SSLContext sslContext = new

SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy()

{

//信任所有

public

boolean isTrusted(X509Certificate[] chain,

String

authType) throws CertificateException {

return

true;

}

}).build();

SSLConnectionSocketFactory sslsf = new

SSLConnectionSocketFactory(sslContext);

return

HttpClients.custom().setSSLSocketFactory(sslsf).build();

} catch

(KeyManagementException e) {

e.printStackTrace();

} catch

(NoSuchAlgorithmExceptione) {

e.printStackTrace();

} catch

(KeyStoreException e) {

e.printStackTrace();

}

return

HttpClients.createDefault();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值