java调用.net asmx / wcf

时间:2023-03-09 07:36:26
java调用.net asmx / wcf

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

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

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services; namespace WebServiceSample
{
/// <summary>
/// Summary description for asmx_service
/// </summary>
[WebService(Namespace = "http://yjmyzz.cnblogs.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class asmx_service : System.Web.Services.WebService
{ [WebMethod]
public string HelloWorld(String msg)
{
return "Hello " + msg + " !";
}
}
}

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

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Web.Services; namespace WebServiceSample
{
// 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.
// 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.
[ServiceContract(Namespace="http://yjmyzz.cnblogs.com/")]
[ServiceBehavior(Namespace = "http://yjmyzz.cnblogs.com/")]
public class wcf_service
{
[OperationContract]
public String HelloWorld(String msg)
{
return "Hello " + msg + " !";
}
}
}

1.3 web.config采用默认设置:

 <?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5.1" />
<httpRuntime targetFramework="4.5.1" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>

完成后,访问网址为:

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

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

二、java端的调用:

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

 <dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency> <dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency> <dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency> <dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.5</version>
</dependency> <dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>

2.2 asmx web service的调用:

先封装一个方法:

     String callAsmxWebService(String serviceUrl, String serviceNamespace,
String methodName, Map<String, String> params)
throws ServiceException, RemoteException, MalformedURLException { org.apache.axis.client.Service service = new org.apache.axis.client.Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(serviceUrl));
call.setOperationName(new QName(serviceNamespace, methodName)); ArrayList<String> paramValues = new ArrayList<String>();
for (Entry<String, String> entry : params.entrySet()) {
call.addParameter(new QName(serviceNamespace, entry.getKey()),
XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
paramValues.add(entry.getValue());
} call.setReturnType(XMLType.XSD_STRING);
call.setUseSOAPAction(true);
call.setSOAPActionURI(serviceNamespace + methodName); return (String) call.invoke(new Object[] { paramValues.toArray() }); }

然后就可以调用了:

     @Test
public void testCallAsmx() throws RemoteException, ServiceException,
MalformedURLException { Map<String, String> params = new HashMap<String, String>();
params.put("msg", "yjmyzz"); String result = callAsmxWebService(
"http://localhost:16638/asmx-service.asmx",
"http://yjmyzz.cnblogs.com/", "HelloWorld", params); System.out.println(result);
}

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

java调用.net asmx / wcf

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

java调用.net asmx / wcf

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

java调用.net asmx / wcf

然后就能调用啦:

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