[英]Java HTTP POST request to a public https:// web service
I try to send a POST request to a public https:// web service. 我尝试将POST请求发送到公共https:// Web服务。 This is my code snippet: 这是我的代码段:
static void xPostJsonUsingHttpClient()
throws ClientProtocolException, IOException {
CloseableHttpClient client = HttpClients.createDefault();
String anafURL =
"https://webservicesp.anaf.ro/PlatitorTvaRest/api/v2/ws/tva";
HttpPost httpPost = new HttpPost(anafURL);
String json = "[{\"cui\":19,\"data\":\"2017-11-07\"}]";
StringEntity entity = new StringEntity(json,"UTF-8");
entity.setContentType("application/json");
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-Type", "application/json");
httpPost.setEntity(entity);
CloseableHttpResponse response = client.execute(httpPost);
System.out.println(response);
client.close();
}
Being a https:// adress I don't know, what I have to change? 作为不知道的https://地址,我必须更改什么? Postman or SoapUI works fine with it. Postman或SoapUI可以正常使用。
When I run my code, it terminates with error: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 当我运行代码时,它以错误终止: javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求的有效证书路径目标
Have I to download a public key or just I have to make some additional settings? 我要下载公共密钥还是只是必须进行一些其他设置?
Thanks 谢谢
Just guessing but I believe your JVM does not recognize the CA of the certificate that this web server uses. 只是猜测,但我相信您的JVM无法识别此Web服务器使用的证书的CA。 I ran into the same problem a while ago and what helped was the following: 不久前,我遇到了一个相同的问题,以下是什么帮助:
The URL you are trying to connect to, uses "certsign.net" as Certificate Authority. 您尝试连接的URL使用“ certsign.net”作为证书颁发机构。
After that you should be able to connect. 之后,您应该可以连接了。 If not let me know. 如果没有让我知道。
You can use http-request built on apache http api. 您可以使用基于apache http api构建的http-request 。
rivate static final HttpRequest<?> HTTP_REQUEST =
HttpRequestBuilder.createPost("https://webservicesp.anaf.ro/PlatitorTvaRest/api/v2/ws/tva")
.trustAllCertificates()
.trustAllHosts()
.addDefaultHeader(ACCEPT, APPLICATION_JSON.getMimeType())
.addContentType(APPLICATION_JSON)
.build();
@Test
public void test() {
ResponseHandler<?> responseHandler =
HTTP_REQUEST.executeWithBody("[{\"cui\":19,\"data\":\"2017-11-07\"}]");
assertEquals(SC_OK, responseHandler.getStatusCode());
}
Status code is OK. 状态代码确定。 You can use String
or sometype instead of wildcard type if you want to get response body. 如果要获取响应正文,可以使用String
或sometype代替通配符类型。 See the documentation. 请参阅文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.