gpt4 book ai didi

java - 使用 Post 请求在 HTTP 客户端中启用 TLS

转载 作者:太空宇宙 更新时间:2023-11-03 13:36:35 25 4
gpt4 key购买 nike

我想在下面的代码中添加 TLS 1.2,尝试创建套接字但没有成功。有人可以帮忙吗?我们可以在创建客户端后添加它吗?

  private static int executeSOAPRequest(String req, String targetURL)
throws Exception {
PostMethod post = new PostMethod(targetURL);
post.setRequestBody(req);
post.setRequestHeader("Content-type",
"text/xml; characterset=ISO-8859-1");
post.setRequestHeader("SOAPAction", "\"\"");

// prepare HTTP Client
HttpClient client = new HttpClient();
client.getParams().setParameter("SOAPAction", "\"\"");

// Post the request
int respCode = client.executeMethod(post);
System.out.println(post.getResponseBodyAsString());
// If response is not success
if (respCode != 200)
throw new Exception("Executing SOAP request has failed.");
// Convert the response into NOM XML
int resp = 0;
Document doc = nomDocPool.lendDocument();
try {
resp = doc.parseString(post.getResponseBodyAsString());
nomDocPool.returnDocument(doc);
} catch (XMLException e) {
nomDocPool.returnDocument(doc);
//logger.error("Exception while generating SAML : "
//+ e);
throw e;
}
System.out.println("resp: "+resp);
return resp;
}

最佳答案

HttpClient 已经为您处理了 TLS。这是记录在案的:

http://hc.apache.org/httpclient-3.x/sslguide.html

HttpClient provides full support for HTTP over Secure Sockets Layer (SSL) or IETF Transport Layer Security (TLS) protocols by leveraging the Java Secure Socket Extension (JSSE). JSSE has been integrated into the Java 2 platform as of version 1.4 and works with HttpClient out of the box.

您所要做的就是确保您的 targetURL 使用的是 https:// 而不是 http://,然后 HttpClient 为您处理剩下的事情。

关于java - 使用 Post 请求在 HTTP 客户端中启用 TLS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38098471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com