WebService有多种方式调用,在开发一个接口客户端的时候第一步肯定是先用soapUI调用接口了解情况。平时工作中最为简单的方法是生成客户端,有java的wsimport或Axis2的wsdl2java来生成。另外还有一种HttpURLConnection的方式,不需求其它的包就能调用。其实webService本质也是Http请求,只不过请求体是 soap协议的方式。
base64Binary类型一般用于上传附件时用到,也就是将 文件的二进制流转成base64的方式传递给服务端
soapUI
网上太多资料不
转自:Web Service单元测试工具实例介绍之SoapUI
使用Axis2生成客户端
1.生成服务端代码命令
WSDL2Java -uri wsdl文件全路径 -p 包名 -d xmlbeans -s -ss -sd -ssi -o 生成的java代码存放路径
2.生成客户端包代码命令
WSDL2Java -uri wsdl文件全路径 -p 包名 -d xmlbeans -s -o 生成的java代码存放路径
WSDL2Java命令参数说明:
-uri 指定*.wsdl文件,可以带具体路径;
-p 指定生成代码的包名
-d <databinding> : 指定databingding,例如,adb,xmlbean,jibx,jaxme and jaxbri 默认adb
-o 指定生成代码放置的路径;
-ss 表示要生成服务端代码;
-ssi 表示要生成代码中,先生成接口类,再生成实现类;
axis2-1.5.6下载地址
从网上下载axis后,解压到 D:\Apache 目录下面,再在CMD中执行如下命令:
axis2 会默认生成 adb 方式
cd D:\Apache\axis2-1.5.6
set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_80
set AXIS2_HOME=D:\Apache\axis2-1.5.6
WSDL2Java -uri D:\Apache\axis2-1.5.6\CapitalSysWS.xml -p com.client.capital -o capital
在 D:\Apache\axis2-1.5.6 目录下面会多出一个 capital 文件夹,里面就是生成的客户端的java文件
在eclipse里新建一个项目,将axis2-1.5.6/lib里的jar包引入到工程中。再将生成的客户端代码添加进去。
调用客户端
package org.web.service.capitalSys;
import java.rmi.RemoteException;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.rpc.ServiceException;
import org.web.service.capitalSys.CapitalSysWSStub.GenerateBillServiceResponse;
/**
* <b>function:</b>上传文件WebService客户端
* http://localhost/easws/services/CapitalSysWS?wsdl
*/
public class CapiitalSysClient {
public static void main(String[] args) throws RemoteException, ServiceException {
// 使用Fiddler 能监听到
System.setProperty("http.proxyHost", "localhost");
System.setProperty("http.proxyPort", "8888");
String target = "http://localhost/easws/services/CapitalSysWS?wsdl";
CapitalSysWSStub stub = new CapitalSysWSStub(target);
CapitalSysWSStub.GenerateBillService service = new CapitalSysWSStub.GenerateBillService();
service.setXmlReq(xmlReq3()); // 流动资金申请信息
service.setAccessory(generateDataHandler()); // 上传本地文件
GenerateBillServiceResponse res = stub.generateBillService(service);
String str = res.get_return();
System.out.println("============返回结果============");
System.out.println(str);
System.out.println(str);
}
private static DataHandler generateDataHandler() {
String fileName = "readMe.txt";
String filePath = System.getProperty("user.dir") + "\\WebContent\\" + fileName;
DataHandler dataHandler = new DataHandler(new FileDataSource(filePath));
return dataHandler;
}
public static String xmlReq3(){
StringBuffer xml = new StringBuffer();
xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
xml.append("<data>");
xml.append("..."); // 略
xml.append("</data>");
return xml.toString();
}
}
使用HttpURLConnection调用
注意这里的soap 的xml 都不一样,最好是用上面的客户端调用一次,再用Fiddler抓一下包。看包里面是怎么传的,再来写这个。
package org.web.service.capitalSys;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.xml.rpc.ServiceException;
import sun.misc.BASE64Encoder;
/**
* <b>function:</b>上传文件WebService客户端
* http://localhost/easws/services/CapitalSysWS?wsdl
*/
public class CapiitalSysClient2 {
public static void main(String[] args) throws ServiceException, IOException {
String target = "http://localhost/easws/services/CapitalSysWS?wsdl";
StringBuffer sb = new StringBuffer(getBody(xmlReq3(), getByteArrayByFile()));
URL u = new URL(target);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setConnectTimeout(60000);
conn.setReadTimeout(60000);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setDefaultUseCaches(false);
conn.setRequestProperty("Content-Type",
"application/soap+xml; charset=UTF-8; action=\"urn:generateBillService\"");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestMethod("POST");
OutputStream output = conn.getOutputStream();
if (null != sb) {
byte[] b = sb.toString().getBytes("UTF-8");
output.write(b, 0, b.length);
}
output.flush();
output.close();
sb = new StringBuffer();
InputStream input = conn.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(input, "UTF-8"));
int c = -1;
while (-1 != (c = bufferedReader.read())) {
sb.append((char) c);
}
bufferedReader.close();
String result = sb.toString().replaceAll("<", "<").replaceAll(">", ">").replaceAll(""", "\"");
System.out.println(result);
}
public static String getBody(String data, String binary) {
StringBuffer xml = new StringBuffer();
xml.append("<?xml version='1.0' encoding='UTF-8'?>");
xml.append("<soapenv:Envelope xmlns:soapenv=\"http://www.w3.org/2003/05/soap-envelope\">");
xml.append("<soapenv:Body><ns1:generateBillService xmlns:ns1=\"http://services.beews.zte.com\">");
xml.append("<ns1:xmlReq><![CDATA[");
xml.append("%s");
xml.append("]]></ns1:xmlReq>");
xml.append("<ns1:accessory>");
xml.append("%s");
xml.append("</ns1:accessory>");
xml.append("</ns1:generateBillService></soapenv:Body></soapenv:Envelope>");
return String.format(xml.toString(), data, binary);
}
// public static void main(String[] args) throws IOException {
// System.out.println(CapiitalSysClient2.getByteArrayByFile());
// }
private static String getByteArrayByFile() throws IOException {
sun.misc.BASE64Encoder base64Encoder = new BASE64Encoder();
String fileName = "readMe.txt";
String filePath = System.getProperty("user.dir") + "\\WebContent\\" + fileName;
File file = new File(filePath);
InputStream input = new FileInputStream(file);
byte[] byt = new byte[input.available()];
input.read(byt);
return base64Encoder.encode(byt);
}
// 对外其他报账单
public static String xmlReq3() {
StringBuffer xml = new StringBuffer();
xml.append("<data>");
xml.append("..."); // 略
xml.append("</data>");
return xml.toString();
}
}
wsdl文件
CapitalSysWS.xml:
<?xml version="1.0" encoding="UTF-8"?><wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://services.beews.zte.com" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://services.beews.zte.com">
<wsdl:documentation>CapitalSysWS</wsdl:documentation>
<wsdl:types>
<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://services.beews.zte.com">
<xs:element name="generateBillService">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="xmlReq" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="accessory" nillable="true" type="xs:base64Binary"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="generateBillServiceResponse">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="return" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</wsdl:types>
<wsdl:message name="generateBillServiceRequest">
<wsdl:part name="parameters" element="ns:generateBillService"/>
</wsdl:message>
<wsdl:message name="generateBillServiceResponse">
<wsdl:part name="parameters" element="ns:generateBillServiceResponse"/>
</wsdl:message>
<wsdl:portType name="CapitalSysWSPortType">
<wsdl:operation name="generateBillService">
<wsdl:input message="ns:generateBillServiceRequest" wsaw:Action="urn:generateBillService"/>
<wsdl:output message="ns:generateBillServiceResponse" wsaw:Action="urn:generateBillServiceResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="CapitalSysWSSoap11Binding" type="ns:CapitalSysWSPortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="generateBillService">
<soap:operation soapAction="urn:generateBillService" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="CapitalSysWSSoap12Binding" type="ns:CapitalSysWSPortType">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="generateBillService">
<soap12:operation soapAction="urn:generateBillService" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="CapitalSysWSHttpBinding" type="ns:CapitalSysWSPortType">
<http:binding verb="POST"/>
<wsdl:operation name="generateBillService">
<http:operation location="CapitalSysWS/generateBillService"/>
<wsdl:input>
<mime:content type="text/xml" part="generateBillService"/>
</wsdl:input>
<wsdl:output>
<mime:content type="text/xml" part="generateBillService"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="CapitalSysWS">
<wsdl:port name="CapitalSysWSHttpSoap11Endpoint" binding="ns:CapitalSysWSSoap11Binding">
<soap:address location="http://localhost:80/easws/services/CapitalSysWS.CapitalSysWSHttpSoap11Endpoint/"/>
</wsdl:port>
<wsdl:port name="CapitalSysWSHttpSoap12Endpoint" binding="ns:CapitalSysWSSoap12Binding">
<soap12:address location="http://localhost:80/easws/services/CapitalSysWS.CapitalSysWSHttpSoap12Endpoint/"/>
</wsdl:port>
<wsdl:port name="CapitalSysWSHttpEndpoint" binding="ns:CapitalSysWSHttpBinding">
<http:address location="http://localhost:80/easws/services/CapitalSysWS.CapitalSysWSHttpEndpoint/"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>