欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java调用WebService .net发布的接口,

来源: javaer 分享于  点击 8292 次 点评:174

java调用WebService .net发布的接口,


因为最近业务上的需求,要对接的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>

 

 

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 } Http Post

 

 

 

另外:如果想尝试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.rmi.RemoteException;
 4 
 5 import javax.xml.namespace.QName;
 6 import javax.xml.rpc.ParameterMode;
 7 import javax.xml.rpc.ServiceException;
 8 import javax.xml.rpc.encoding.XMLType;
 9 import javax.xml.validation.Schema;
10 
11 import org.apache.axis.client.Call;
12 import org.apache.axis.client.Service;
13 import org.apache.axis.soap.SOAPConstants;
14 
15 public class SendSMSUtil {
16 
17     /**
18      * 
19      * @param url
20      *            提供接口的地址
21      * @param methodName
22      *            方法名
23      * @return
24      * @Description: TODO(方法描述)
25      * @author 李志新
26      * @date 2020年1月9日 下午4:55:37
27      */
28     public static void CallService() {
29         //天气预报发布的的url
30         String url = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";    
31         //对应的发布地址
32         String namespace = "http://WebXml.com.cn/";
33         //提供的某个方法
34         String methodName = "getSupportProvince";
35         //soapAction  就是 namespace+methodName 注意中间有有个/
36         String soapActionURI = "http://WebXml.com.cn/getSupportProvince";
37         //new一个服务
38         Service service = new Service();
39         Call call;
40         try {
41             //创建一个Call  然后设置一些信息
42             call = (Call) service.createCall();
43             call.setTargetEndpointAddress(url);
44             call.setSOAPVersion(SOAPConstants.SOAP11_CONSTANTS);
45             call.setEncodingStyle("utf-8");
46             call.setUseSOAPAction(true);
47             call.setSOAPActionURI(soapActionURI);
48             call.setOperationName(new QName(namespace, methodName));
49             //添加参数    byProvinceName 为参数名  XMLType.XSD_STRING 为参数类型   ParameterMode.IN 为入参的意思
50             call.addParameter(new QName(namespace, "byProvinceName"),
51              XMLType.XSD_STRING,ParameterMode.IN);
52             call.addParameter(new QName(namespace, "theCityName"),
53                      XMLType.XSD_STRING,ParameterMode.IN);
54             //返回的参数设置类型  XSD_STRING String类型
55             call.setReturnType(XMLType.XSD_STRING);
56             //请注意代码不完整,直接调用必报错 ,call.invoke() 调用方法    new Object[]{}  参数存放在{}中
57              System.out.println( call.invoke(new Object[]{}));
58         } catch (ServiceException e) {
59             e.printStackTrace();
60         } catch (RemoteException e) {
61             e.printStackTrace();
62         }
63     }
64 }

相关文章

    暂无相关文章
相关栏目:

用户点评