专栏首页菩提树下的杨过java调用.net asmx / wcf

java调用.net asmx / wcf

一、先用asmx与wcf写二个.net web service:

1.1 asmx web服务:asmx-service.asmx.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Services;
 6 
 7 namespace WebServiceSample
 8 {
 9     /// <summary>
10     /// Summary description for asmx_service
11     /// </summary>
12     [WebService(Namespace = "http://yjmyzz.cnblogs.com/")]
13     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
14     [System.ComponentModel.ToolboxItem(false)]
15     // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
16     // [System.Web.Script.Services.ScriptService]
17     public class asmx_service : System.Web.Services.WebService
18     {
19 
20         [WebMethod]
21         public string HelloWorld(String msg)
22         {
23             return "Hello " + msg + " !";
24         }
25     }
26 }

1.2 wcf服务:wcf-service.svc.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Runtime.Serialization;
 5 using System.ServiceModel;
 6 using System.Text;
 7 using System.Web.Services;
 8 
 9 namespace WebServiceSample
10 {
11     // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "wcf_service" in code, svc and config file together.
12     // NOTE: In order to launch WCF Test Client for testing this service, please select wcf-service.svc or wcf-service.svc.cs at the Solution Explorer and start debugging.
13     [ServiceContract(Namespace="http://yjmyzz.cnblogs.com/")]
14     [ServiceBehavior(Namespace = "http://yjmyzz.cnblogs.com/")]
15     public class wcf_service
16     {
17         [OperationContract]
18         public String HelloWorld(String msg)
19         {
20             return "Hello " + msg + " !";
21         }
22     }
23 }

1.3 web.config采用默认设置:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <configuration>
 3   <system.web>
 4     <compilation debug="true" targetFramework="4.5.1" />
 5     <httpRuntime targetFramework="4.5.1" />
 6   </system.web>
 7   <system.serviceModel>
 8     <behaviors>
 9       <serviceBehaviors>
10         <behavior name="">
11           <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
12           <serviceDebug includeExceptionDetailInFaults="false" />
13         </behavior>
14       </serviceBehaviors>
15     </behaviors>
16     <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
17       multipleSiteBindingsEnabled="true" />
18   </system.serviceModel>
19 </configuration>

完成后,访问网址为:

http://localhost:16638/asmx-service.asmx

http://localhost:16638/wcf-service.svc

二、java端的调用:

2.1 pom.xml中先添加以下依赖项:

 1 <dependency>
 2             <groupId>org.apache.axis</groupId>
 3             <artifactId>axis</artifactId>
 4             <version>1.4</version>            
 5         </dependency>
 6 
 7         <dependency>
 8             <groupId>org.apache.axis</groupId>
 9             <artifactId>axis-jaxrpc</artifactId>
10             <version>1.4</version>            
11         </dependency>
12 
13         <dependency>
14             <groupId>wsdl4j</groupId>
15             <artifactId>wsdl4j</artifactId>
16             <version>1.6.3</version>            
17         </dependency>
18 
19         <dependency>
20             <groupId>commons-discovery</groupId>
21             <artifactId>commons-discovery</artifactId>
22             <version>0.5</version>            
23         </dependency>
24 
25         <dependency>
26             <groupId>commons-logging</groupId>
27             <artifactId>commons-logging</artifactId>
28             <version>1.1.3</version>            
29         </dependency>

2.2 asmx web service的调用:

先封装一个方法:

 1     String callAsmxWebService(String serviceUrl, String serviceNamespace,
 2             String methodName, Map<String, String> params)
 3             throws ServiceException, RemoteException, MalformedURLException {
 4 
 5         org.apache.axis.client.Service service = new org.apache.axis.client.Service();
 6         Call call = (Call) service.createCall();
 7         call.setTargetEndpointAddress(new java.net.URL(serviceUrl));
 8         call.setOperationName(new QName(serviceNamespace, methodName));
 9 
10         ArrayList<String> paramValues = new ArrayList<String>();
11         for (Entry<String, String> entry : params.entrySet()) {
12             call.addParameter(new QName(serviceNamespace, entry.getKey()),
13                     XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
14             paramValues.add(entry.getValue());
15         }
16 
17         call.setReturnType(XMLType.XSD_STRING);
18         call.setUseSOAPAction(true);
19         call.setSOAPActionURI(serviceNamespace + methodName);
20 
21         return (String) call.invoke(new Object[] { paramValues.toArray() });
22 
23     }

然后就可以调用了:

 1     @Test
 2     public void testCallAsmx() throws RemoteException, ServiceException,
 3             MalformedURLException {        
 4 
 5         Map<String, String> params = new HashMap<String, String>();
 6         params.put("msg", "yjmyzz");
 7 
 8         String result = callAsmxWebService(
 9                 "http://localhost:16638/asmx-service.asmx",
10                 "http://yjmyzz.cnblogs.com/", "HelloWorld", params);
11 
12         System.out.println(result);
13     }

2.3 wcf服务的调用:

这个要借助IDE环境生成代理类(或者用命令JAVA_HOME\bin\wsimport.exe -s  c:\test\javasrc http://xxx.com/xxx.svc?wsdl)

eclipse环境中,project上右击->New->Other->Web Service Client

输入wsdl的地址,注意:wcf会生成二个wsdl的地址,用xxx?singleWsdl这个,如下图:

直接Finish,会生成一堆java文件:

然后就能调用啦:

1     @Test
2     public void testCallWcf() throws RemoteException, ServiceException,
3             MalformedURLException {        
4         
5         Wcf_service_ServiceLocator locator = new Wcf_service_ServiceLocator();
6         locator.setBasicHttpBinding_wcf_serviceEndpointAddress("http://localhost:16638/wcf-service.svc");
7         System.out.println(locator.getBasicHttpBinding_wcf_service().helloWorld("jimmy"));         
8     }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 利用message queue实现aspx与winform通信, 并附完整示例

    先在本机(本文中的计算机名为jimmyibm)安装message queue(消息队列),别告诉我你不会安装.(就跟安装IIS差不多的) 安装好后,打开 "计算...

    菩提树下的杨过
  • [原创]WCF入门级使用教程(转载请注明出处)

    开发环境:vs2008英文版(SP1) + IIS + Windows2003 整个解决方案有4个项目 01.WCF ---Class Libary项目,用于...

    菩提树下的杨过
  • (Head First 设计模式)学习笔记(2) --观察者模式(气象站实例)

    应用概述: 某气象站通过传感器实时测量气温/湿度/压力等数据,要求设计一个系统,能让多种类型的公告栏自动更新这些数据(本例中有二类公告板:实时显示气温/温度...

    菩提树下的杨过
  • 日渐兴隆的软件漏洞“灰色市场”渐已显现

    2月11日是国际互联网安全日,这一起源于欧洲的活动日如今影响力遍及全球。在可预见的未来,国家和地区自己的社交网络、云存储系统和搜索引擎将有所增加...

    安恒信息
  • Error response from daemon: rpc error: code = Unknown desc = name conflicts with

    docker service create --mode global --name logspout gliderlabs/logspout

    周小董
  • 泛型

    mathor
  • Zookeeper应用之——栅栏(barrier)

    barrier的作用是所有的线程等待,知道某一时刻,锁释放,所有的线程同时执行。举一个生动的例子,比如跑步比赛,所有 运动员都要在起跑线上等待,直到枪声响后,所...

    小忽悠
  • [开源]基于Arduino+OpenCV的颠球机器人

    看完了,是不是觉得似曾相识呢?这不就是2017年的电赛B题目的升级版吗?17年电赛那个题目,常用的解决方案是把摄像头放在上面,而且小球只在平板上进行二维运动。这...

    单片机点灯小能手
  • 修复Android中Navigation Bar遮挡PopupWindow的问题

    最近遇到了一个问题,关于Navigation Bar遮挡PopupWindow的问题,问题不难,粗略做一点总结。

    技术小黑屋
  • Android全屏的两种方法 博客分类: Android小技巧 AndroidXML

     然后在manifest.xml中,为想要全屏的activity加上android:theme="@style/Theme.NoTitle_FullScreen...

    chroya

扫码关注云+社区

领取腾讯云代金券