springboot怎么调用接口https://10.195.1.130:8243/services/ZTF_XYS_wSBusinessInfoService_v1?wsdl
时间: 2023-08-10 09:07:14 浏览: 510
Python库 | ztf_plan_obs-0.24-py3-none-any.whl
在Spring Boot中调用HTTPS接口,可以使用Java原生的HttpURLConnection或Apache的HttpClient。以下是使用Apache HttpClient的示例代码:
1. 在pom.xml文件中添加Apache HttpClient的依赖:
```xml
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
</dependency>
```
2. 在代码中使用HttpClient发送HTTPS请求:
```java
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import javax.net.ssl.SSLContext;
import java.io.IOException;
public class HttpsClientExample {
public static void main(String[] args) throws IOException {
// create SSL context
SSLContext sslContext = SSLContexts.createDefault();
// create SSL socket factory with hostname verification disabled
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
// create HTTP client with SSL socket factory
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslSocketFactory).build();
// create HTTP GET request
HttpGet httpGet = new HttpGet("https://10.195.1.130:8243/services/ZTF_XYS_wSBusinessInfoService_v1?wsdl");
// execute HTTP request and get response
CloseableHttpResponse response = httpClient.execute(httpGet);
// process HTTP response
// ...
// close HTTP client and response
response.close();
httpClient.close();
}
}
```
注意:在示例代码中,我们使用了NoopHostnameVerifier.INSTANCE来禁用主机名验证,这是由于HTTPS证书可能是自签名的,所以主机名验证会失败。在生产环境中,应该使用合适的主机名验证方式。
阅读全文