JAVA代码okhttp调用webService接口
时间: 2024-03-31 11:33:50 浏览: 171
您可以使用以下代码使用OkHttp调用Web服务接口:
```java
OkHttpClient client = new OkHttpClient();
String xml = "your SOAP request body here";
RequestBody body = RequestBody.create(MediaType.parse("text/xml;charset=utf-8"), xml);
Request request = new Request.Builder()
.url("your web service url here")
.post(body)
.build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
String result = response.body().string();
// process result
} else {
// handle error
}
```
在上面的代码中,您需要将“your SOAP request body here”替换为您的SOAP请求正文,并将“your web service url here”替换为您的Web服务URL。
另外,您需要添加以下依赖项到您的项目中:
```xml
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.0</version>
</dependency>
```
阅读全文