使用Apache提供的HttpClient发送https请求

/**

* @param sendXml
*            需要发送的报文字符串
* @param requestUrl
*            请求url地址
* @return 响应报文字符串
*/
public static String sendSOAP(String sendXml, String requestUrl) {
String responseBodySOAP = null;
byte[] requestBytes;
try {
// 请求发送前给系统添加参数,JKS证书
System.setProperty("javax.net.ssl.trustStore", csj.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "123456");
System.setProperty("javax.net.ssl.keyStoreType", "JKS");
System.setProperty("javax.net.ssl.keyStore",csj.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "123456");
requestBytes = sendXml.getBytes();
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(requestUrl);
InputStream inputStream = new ByteArrayInputStream(requestBytes, 0, requestBytes.length);
RequestEntity requestEntity = new InputStreamRequestEntity(inputStream, requestBytes.length,
"application/soap+xml; charset=utf-8");
postMethod.setRequestEntity(requestEntity);
System.out.println("请求报文:" + sendXml);
httpClient.executeMethod(postMethod);
// responseBodySOAP = postMethod.getResponseBodyAsString();


BufferedReader br = new BufferedReader(
new InputStreamReader(postMethod.getResponseBodyAsStream(), "utf-8"));


String responseLine = "";
StringBuffer sb = new StringBuffer();
for (responseLine = br.readLine(); responseLine != null; responseLine = br.readLine()) {
sb.append(responseLine);
}
br.close();
responseBodySOAP = sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("响应报文:" + responseBodySOAP);

return responseBodySOAP;
}
以下是使用 Apache HttpClient 发送 HTTP 和 HTTPS 请求的示例代码: 1. 发送 HTTP 请求 ```java import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClients; public class HttpClientExample { public static void main(String[] args) throws Exception { HttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("http://www.example.com"); HttpResponse httpResponse = httpClient.execute(httpGet); System.out.println(httpResponse.getStatusLine().getStatusCode()); // 打印响应状态码 } } ``` 2. 发送 HTTPS 请求 ```java import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.conn.ssl.SSLContextBuilder; import org.apache.http.impl.client.HttpClients; import javax.net.ssl.SSLContext; public class HttpClientExample { public static void main(String[] args) throws Exception { SSLContext sslContext = new SSLContextBuilder() .loadTrustMaterial(null, (certificate, authType) -> true) .build(); HttpClient httpClient = HttpClients.custom() .setSSLContext(sslContext) .setSSLHostnameVerifier(new NoopHostnameVerifier()) .build(); HttpGet httpGet = new HttpGet("https://www.example.com"); HttpResponse httpResponse = httpClient.execute(httpGet); System.out.println(httpResponse.getStatusLine().getStatusCode()); // 打印响应状态码 } } ``` 注意事项: - HTTPS 请求需要设置 SSLContext 和 SSLHostnameVerifier; - SSLContext 的 TrustManager 应该接受所有证书,这不安全,建议修改为验证证书; - SSLHostnameVerifier 应该使用严格的 HostnameVerifier,这里为了简单只使用了 NoopHostnameVerifier。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值