方法一:通过HTTP访问接口,根据webservice的wsdl说明来设置参数:
public static String sendSoapPost(String url, String xml,
String contentType, String soapAction) {
HttpURLConnection httpConn = null;
OutputStream out = null;
String result = "";
try {
httpConn = (HttpURLConnection) new URL(url).openConnection();
httpConn.setRequestProperty("Content-Type", contentType);
if (null != soapAction) {
httpConn.setRequestProperty("SOAPAction", soapAction);
}
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.connect();
// 获取输出流对象
out = httpConn.getOutputStream();
// 将要提交服务器的SOAP请求字符流写入输出流
httpConn.getOutputStream().write(xml.getBytes());
out.flush();
out.close();
int code = httpConn.getResponseCode();
StringBuffer sb1 = new StringBuffer();
if (code == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader( new InputStreamReader(httpConn.getInputStream(),"UTF-8"));
String inputLine;
while (null != (inputLine = reader.readLine())) {
Pattern pattern = Pattern.compile("<GetElecReceiptListResult>(.*)</GetElecReceiptListResult>");
Matcher matcher = pattern.matcher(inputLine);
while (matcher.find()) {
result = matcher.group(1);
}
}
if (null != reader) {
reader.close();
}
} else {
InputStream errorStream = httpConn.getErrorStream();
BufferedReader reader = new BufferedReader( new InputStreamReader(errorStream,"UTF-8"));
String inputLine;
// 解析获取结果集
while (null != (inputLine = reader.readLine())) {
Pattern pattern = Pattern.compile("<GetElecReceiptListResult>(.*)</GetElecReceiptListResult>");
Matcher matcher = pattern.matcher(inputLine);
while (matcher.find()) {
result = matcher.group(1);
}
}
if (null != reader) {
reader.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
上述方法调用
public static void main(String[] args) {
JSONObject json = new JSONObject();
json.put("startDate", "20211207");
json.put("endDate", "20211207");
String param = json.toJSONString();
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
" <soap12:Body>\n" +
" <GetElecReceiptList xmlns=\"http://tempuri.org/\">\n" +
" <filterStr>" + param + "</filterStr>\n" +
" </GetElecReceiptList>\n" +
" </soap12:Body>\n" +
"</soap12:Envelope>";
// server域名地址,为了统一规范,一般都是这个域名
String soapaction = "http://tempuri.org/";
// 方法名
try {
// webService链接地址
String url = "webService链接地址";
String contentType = "application/soap+xml; charset=utf-8";
String resultXml = sendSoapPost(url, xml, contentType,
soapaction);
System.out.println(resultXml);
} catch (Exception e) {
System.err.println(e.toString());
}
}
方法二:通过axis调用,需要引入相应jar包
<dependency> <groupId>org.apache.axis</groupId> <artifactId>axis</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>axis</groupId> <artifactId>axis-jaxrpc</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-discovery</groupId> <artifactId>commons-discovery</artifactId> <version>0.5</version> </dependency> <dependency> <groupId>wsdl4j</groupId> <artifactId>wsdl4j</artifactId> <version>1.6.3</version> </dependency>
public static void main(String[] args) {
// server域名地址,为了统一规范,一般都是这个域名
String soapaction = "http://tempuri.org/";
// 方法名
String methodName = "方法名";
try {
// webService链接地址
String url = "webService链接地址";
// 用户提供测试的两个参数
JSONObject json = new JSONObject();
json.put("startDate","20211206");
json.put("endDate","20211206");
String param = json.toJSONString();
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(url);
// 设置要调用哪个方法
call.setOperationName(new QName(soapaction,methodName));
// 设置要传递的参数名
call.addParameter(new QName(soapaction,"filterStr"), XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
// 提供标准类型
call.setReturnType(XMLType.SOAP_STRING);
call.setUseSOAPAction(true);
call.setSOAPActionURI(soapaction + methodName);
// 调用方法并传递参数
String invoke = (String)call.invoke(new Object[]{param});
System.out.println(invoke);
} catch (Exception e) {
System.err.println(e.toString());
}
}