我想用 apache httpclient 调用 https post 方法。 我在互联网上找不到解决方案。 问题是当我调用方法 https 更改为 http 时,我不知道为什么?!
我尝试了很多方法,但都将url中的https更改为http。
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(20000)
.setSocketTimeout(20000)
.build();
StringEntity entity = new StringEntity("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
" <soap:Body>\n" +
" <GetComplaintData xmlns=\"http://tempuri.org/\">\n" +
" <UserName></UserName>\n" +
" <Password></Password>\n" +
" <TrackingCode>"+complaintCode+"</TrackingCode>\n" +
" </GetComplaintData>\n" +
" </soap:Body>\n" +
"</soap:Envelope>", "UTF-8");
HttpUriRequest request = RequestBuilder.create('POST')
.setConfig(requestConfig)
.setUri("https://195.cra.ir:8085/Complaint.asmx?op=GetComplaintData")
.setHeader(HttpHeaders.CONTENT_TYPE, "text/xml;charset=UTF-8")
.setEntity(entity)
.build();
HttpClientBuilder.create().build().withCloseable {httpClient ->
httpClient.execute(request).withCloseable {response ->
String res = "RESPONSE:" + "\n" + response.getStatusLine() + "\n" + "Headers: " +
response.getAllHeaders() + "\n" +
(response.getEntity() != null ? EntityUtils.toString(response.getEntity()) : "") + "\n";
println(res.toString()+"_________________________-")
}
}
我希望 url 不要将 https 更改为 http。 抱歉,如果我解释得不好。谢谢
最佳答案
只需搜索:apache http client post
,您就可以在网络上找到大量示例。无论如何,这应该适合您,具体取决于您使用的版本:
final String envelope = "<soap:Envelope ... </soap:Envelope>";
final StringEntity entity = new StringEntity(envelope, StandardCharsets.UTF_8);
entity.setChunked(true);
final HttpPost httpPost = new HttpPost("https://your.url?soap=Operation");
httpPost.setEntity(entity);
httpPost.addHeader("SOAPAction", "YourSOAPActionHere");
final CloseableHttpResponse response = HttpClients.createDefault().execute(httpPost);
response.getStatusLine();
EntityUtils.toString(response.getEntity());
关于java - Apache httpPost 将 url 中的 https 更改为 http,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57171748/