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 }
View Code

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 }
View Code

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>
View Code

完成后,访问网址为:

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>
View Code

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     }
View Code

然后就可以调用了:

 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     }
View Code

 

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     }
View Code

 

posted @ 2014-11-12 13:52  菩提树下的杨过  阅读(3224)  评论(0编辑  收藏  举报