java - Apache httpPost 将 url 中的 https 更改为 http

标签 java

我想用 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/

相关文章:

java - 我怎样才能知道在Android studio中选择了哪个单选按钮

java - Google Sheet API V4 Java,从列号获取列字母

java - java中通过套接字进行客户端服务器身份验证

java - 我最多只能打印 "red: 1",之后我的程序由于使用 break 语句而结束

java - Servlet 全局化 NumberFormat.getCurrencyInstance(Locale.JAPAN) 无法正常工作

java - 在 Java 中一次从控制台读取一个单词

java - web.xml 和 spring mvc 配置文件中处理错误的区别

java - 需要了解许可证 key 算法

java - Spring Boot 无法更新 azure cosmos db(MongoDb) 上的分片集合

java - CSV 文件,二进制格式更快?最快的搜索?