今天需要对接一个接口,接口是遵循WebService协议的,然后我就懵逼了,没用过。于是就去百度了好久。于是就有了今天的博客。
Webservice是基于SOAP协议的请求,SOAP协议是xml格式的。所以请求参数和回执全部都是xml。
获取命名空间和参数等信息
一般webservice接口只会给一个xxx.wsdl文件
如:http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl
从这个文件中我们可以获取到接口所需的参数,我们可以使用soapUI软件来进行查看
然后新增一个项目
名称随便写 wsdl就写url 点击ok
然后点击里面的Request1就可以看到参数信息了,其中有个?号,我们到时候把?替换成xml格式的参数就可以发送了。
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://www.cvicse.com/service/">
<soapenv:Header/>
<soapenv:Body>
<ser:syncServiceStationOperationRequest>
<in>
?
</in>
</ser:syncServiceStationOperationRequest>
</soapenv:Body>
</soapenv:Envelope>
请求
pom文件
<dependency>
<groupId>org.apache.ws.commons.axiom</groupId>
<artifactId>axiom-api</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>org.apache.ws.commons.axiom</groupId>
<artifactId>axiom-impl</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency>
<dependency>
<groupId>org.apache.ws.xmlschema</groupId>
<artifactId>xmlschema-core</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.1</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
请求
public static void main(String[] args) throws Exception {
String str =new String("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://www.cvicse.com/service/\"><soapenv:Header/><soapenv:Body><ser:syncServiceStationOperationRequest><in><![CDATA[这里放入xml参数]]></in></ser:syncServiceStationOperationRequest></soapenv:Body></soapenv:Envelope>".getBytes(),"UTF-8");
System.out.println(str);
HttpClient httpClient = new HttpClient();
httpClient.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT,3000);
httpClient.getParams().setParameter(HttpConnectionParams.SO_TIMEOUT, 1000);
PostMethod post = new PostMethod("http://xxxxxx/services/syncServiceStation?wsdl");
post.setRequestHeader("Content-Type","text/xml;charset=utf-8");
RequestEntity re = new InputStreamRequestEntity(new ByteArrayInputStream(str.getBytes()));
post.setRequestEntity(re);
BufferedReader in = null;
try {
int status =httpClient.executeMethod(post);
if (status == 200) {
System.out.println("成功");
InputStream io =post.getResponseBodyAsStream();
InputStreamReader is = new InputStreamReader(io, "UTF-8");
in = new BufferedReader(is);
String inputLine;
StringBuilder result = new StringBuilder();
while ((inputLine = in.readLine()) != null)
{
result.append(inputLine);
}
String a =result.toString().replace("<", "<").replace(">", ">");
System.out.println(a);
} else {
System.out.println("失败");
System.out.println(post.getResponseBodyAsString());
}
} catch (IOException e) {
} finally {
if (in != null){
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
post.releaseConnection();
httpClient.getHttpConnectionManager().closeIdleConnections(0);
}
}