<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.3.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.3.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.3.5</version>
</dependency>
第二个方法
package com.zcst.modules.doc.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
@Slf4j
public class UserClient {
public static void getcreate(String result, String url) {
JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
// 创建动态客户端
Client client = factory.createClient(url);
// 需要密码的情况需要加上用户名和密码
// client.getOutInterceptors().add(new
// ClientLoginInterceptor(USER_NAME,PASS_WORD));
HTTPConduit conduit = (HTTPConduit) client.getConduit();
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setConnectionTimeout(2000); // 连接超时
httpClientPolicy.setAllowChunking(false); // 取消块编码
httpClientPolicy.setReceiveTimeout(120000); // 响应超时
conduit.setClient(httpClientPolicy);
// client.getOutInterceptors().addAll(interceptors);//设置拦截器
try {
Object[] objects = null;
// invoke("方法名",参数1,参数2,参数3....);
objects = client.invoke("postArchives", result);
System.out.println(objects[0]);
} catch (Exception e) {
e.printStackTrace();
log.info("解析接口报的错:" + e);
} finally {
log.info("执行到该方法:");
}
System.out.println(client);
}
public static Map<String, Object> webServiceRequestUtil(String requesturl, String soapXML) throws IOException {
HttpURLConnection connection = null;
OutputStream os = null;
Map<String, Object> reslut = new HashMap<>();
try {
//1:创建服务地址
URL url = new URL(requesturl);
//2:打开到服务地址的一个连接
connection = (HttpURLConnection) url.openConnection();
//3:设置连接参数
//3.1设置发送方式:POST必须大写
connection.setRequestMethod("POST");
//3.2设置数据格式:Content-type
connection.setRequestProperty("content-type", "application/xml");
//3.3设置输入输出,新创建的connection默认是没有读写权限的,
connection.setDoInput(true);
connection.setDoOutput(true);
//4:组织SOAP协议数据,发送给服务端
os = connection.getOutputStream();
os.write(soapXML.getBytes());
} catch (IOException e) {
log.info("链接webservice出错!url为:" + requesturl + ";exception:" + e);
}
StringBuilder sb = new StringBuilder();
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
String temp = null;
//5:接收服务端的响应
int responseCode = connection.getResponseCode();
log.info("服务器响应码----》" + responseCode);
if (200 == responseCode) {//表示服务端响应成功
is = connection.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
while ((temp = br.readLine()) != null) {
sb.append(temp);
}
reslut.put("code", responseCode);
reslut.put("msg", sb.toString());
} else {
is = connection.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
while ((temp = br.readLine()) != null) {
sb.append(temp);
}
reslut.put("code", responseCode);
reslut.put("msg", sb.toString());
}
} catch (IOException e) {
log.info("返回结果解析出错--> " + e);
} finally {
if (!Objects.isNull(br)) {
br.close();
}
if (!Objects.isNull(isr)) {
isr.close();
}
if (!Objects.isNull(is)) {
is.close();
}
if (!Objects.isNull(os)) {
os.close();
}
}
return reslut;
}
}
String soap = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.webservice.archive.component.ces.com/\">" +
" <soapenv:Header/>" +
" <soapenv:Body>" +
" <ser:postArchives>" +
" <!--Optional:-->" +
" <arg0>YGD-J0211-20220704150157</arg0>" +
" <!--Optional:-->" +
" <arg1>YGD-J0211-20220704150157</arg1>" +
" <!--Optional:-->" +
" <arg2>J0211/J0211/2022/YGD-J0211-20220704150157.zip</arg2>" +
" <!--Optional:-->" +
" <arg3>张三</arg3>" +
" <!--Optional:-->" +
" <arg4>972</arg4>" +
" <!--Optional:-->" +
" <arg5>J0211</arg5>" +
" <!--Optional:-->" +
" <arg6>河南省生态环境厅</arg6>" +
" <!--Optional:-->" +
" <arg7>2022</arg7>" +
" <!--Optional:-->" +
" <arg8>河南省生态环境厅综合办公平台</arg8>" +
" </ser:postArchives>" +
" </soapenv:Body>" +
"</soapenv:Envelope>";