因为最近业务上的需求,要对接的WebService,调用他们的接口正文如下:
环境:java调用C#的WebService接口,因为对接的人一问三不知,实在是没办法只能自己来搞。
个人建议搞一个SoapUI测试工具,需要破解,先测试接口是否调的通再开搞,这个百度可以搜索得到
正文开始喽
1.首先了解各种调用的方法,百度有懒得搜,最简单和广泛的就是最新的使用eclipse的webservice生成本地的代理类,然后就像调用本地方法一样去调用
2.另外就是远程调用,本人第一次是使用的 axis new Service 使用 Call 调用,最后是因为返回类型的结果序列化失败,有人说是因为源码原因,我是没有成功于是就换了一种调用方式,那就是Http Post
HttpClient 工具类方法 和测试main如下
Mavean包如下:
<!--HttpClient -->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
另外:如果想尝试axis方法的兄弟我这里提供一下包,当时找资料因为这个包的原因找了好久,太浪费时间。
直接Mavean仓库找或者如下:
<!-- 一下为axis调用webservice接口所需要的包 -->
<!-- https://mvnrepository.com/artifact/org.apache.axis/axis -->
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/axis/axis-jaxrpc -->
<dependency>
<groupId>axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/axis/axis-wsdl4j -->
<dependency>
<groupId>axis</groupId>
<artifactId>axis-wsdl4j</artifactId>
<version>1.5.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-discovery/commons-discovery -->
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.5</version>
</dependency>
1 package com.nh.qf.utils; 2 3 import java.io.BufferedReader; 4 import java.io.InputStreamReader; 5 import java.io.OutputStream; 6 import java.net.HttpURLConnection; 7 import java.net.URL; 8 9 public class HttpUtil { 10 11 public static String sendSoapPost(String url, String xml, 12 String contentType, String soapAction) { 13 String urlString = url; 14 HttpURLConnection httpConn = null; 15 OutputStream out = null; 16 String returnXml = ""; 17 try { 18 httpConn = (HttpURLConnection) new URL(urlString).openConnection(); 19 httpConn.setRequestProperty("Content-Type", contentType); 20 if (null != soapAction) { 21 httpConn.setRequestProperty("SOAPAction", soapAction); 22 } 23 httpConn.setRequestMethod("POST"); 24 httpConn.setDoOutput(true); 25 httpConn.setDoInput(true); 26 httpConn.connect(); 27 out = httpConn.getOutputStream(); // 获取输出流对象 28 httpConn.getOutputStream().write(xml.getBytes("UTF-8")); // 将要提交服务器的SOAP请求字符流写入输出流 29 out.flush(); 30 out.close(); 31 int code = httpConn.getResponseCode(); // 用来获取服务器响应状态 32 String tempString = null; 33 StringBuffer sb1 = new StringBuffer(); 34 if (code == HttpURLConnection.HTTP_OK) { 35 BufferedReader reader = new BufferedReader( 36 new InputStreamReader(httpConn.getInputStream(), 37 "UTF-8")); 38 while ((tempString = reader.readLine()) != null) { 39 sb1.append(tempString); 40 } 41 if (null != reader) { 42 reader.close(); 43 } 44 } else { 45 BufferedReader reader = new BufferedReader( 46 new InputStreamReader(httpConn.getErrorStream(), 47 "UTF-8")); 48 // 一次读入一行,直到读入null为文件结束 49 while ((tempString = reader.readLine()) != null) { 50 sb1.append(tempString); 51 } 52 if (null != reader) { 53 reader.close(); 54 } 55 } 56 // 响应报文 57 returnXml = sb1.toString(); 58 } catch (Exception e) { 59 e.printStackTrace(); 60 } 61 return returnXml; 62 } 63 64 public static void main(String[] args) { 65 String url = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"; 66 67 StringBuilder sb = new StringBuilder(""); 68 sb.append( 69 "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" ") 70 .append("xmlns:web=\"http://WebXml.com.cn/\"><soapenv:Header/><soapenv:Body>") 71 .append("<web:getSupportProvince/></soapenv:Body></soapenv:Envelope>"); 72 /* 73 * sb.append( 74 * "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " 75 * ) .append( 76 * "xmlns:web=\"http://WebXml.com.cn/\"><soapenv:Header/><soapenv:Body><web:getSupportCity>" 77 * ) .append( 78 * "<web:byProvinceName>河北</web:byProvinceName></web:getSupportCity></soapenv:Body></soapenv:Envelope>" 79 * ); 80 */ 81 String dataXml = sb.toString(); 82 String contentType = "text/xml; charset=utf-8"; 83 String soapAction = "http://WebXml.com.cn/getSupportProvince"; 84 // String soapAction = 85 // "\"document/http://pengjunnlee.com/CustomUI:GetWeatherById\""; 86 String resultXml = HttpUtil.sendSoapPost(url, dataXml, contentType, 87 soapAction); 88 System.out.println(resultXml); 89 } 90 }