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

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

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

然后就能调用啦:
@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"));
}
java调用.net asmx / wcf的更多相关文章
- Java调用Webservice(asmx)的几个例子
Java调用Webservice(asmx)的几个例子 2009-06-28 17:07 写了几个调用例子: 1. import org.apache.axis.client.*;import org ...
- 用java调用.net的wcf其实还是很简单的
前些天和我们的一个邮件服务商对接,双方需要进行一些通讯,对方是java团队,而作为.net团队的我们,只能公布出去的是一个wcf的basicbinding,想不 到问题来了,对方不知道怎么去调用这 ...
- PHP&Java 调用C#的WCF
步骤一:用C#声明WCF [ServiceContract] public interface IService1 { [OperationContract] void DoWork(); [Oper ...
- java调用.net asmx服务
有时候,在java下开发会调用一下.net下写的服务,看网上有各种方法,但总是不成功,总结下自己测试过能调用成功的方式: 1. 原始方式http-soap public static String p ...
- java 调用webservice (asmx) 客户端开发示例
这是本人第一次写博客,其实就是自己做个笔记,写的很粗糙,也希望能给跟我遇到同样问题的你一点帮助. 因为最近有个项目要调用webservice接口,之前接触的都是Java开发服务端和客户端的接口,开发前 ...
- JAVA调用WCF
Java环境下生成代理类的工具有很多,如wsdl2Java,wsimport 等.本文中使用的工具是wsimport. 1.wsdl2Java 生成命令实例: wsdl2Java -p package ...
- Web循环监控Java调用 / Java调用.net wcf接口
背景介紹 (Background Introduction) 目前有一些报表客户抱怨打不开 报表执行过程过长,5.8.10秒甚至更多 不能及时发现和掌握服务器web站点情况 用戶需求(User Req ...
- 【转】java调用webservice
互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的Web应用中显示,下面就以获取天气预报数据和查询国内手机号码归属地为 ...
- 分享:根据webservice WSDL地址自动生成java调用代码及JAR包
分享:根据webservice WSDL地址自动生成java调用代码及JAR包使用步骤:一.安装java 并配置JAVA_HOME 及 path二.安装ANT 并配置ANT_HOME三.解压WsdlT ...
随机推荐
- php设计模式 适配器模式
适配器模式,可以将截然不同的函数接口封装成统一的API: 应用举例,PHP的数据库操作有Mysql.Mysqli.pdo三种,可以用适配器模式统一成一致,类似的场景还有cache适配器,将memcac ...
- 学习 jsonp
1.起因 js脚本做ajax异步调用的时候,直接请求普通文件存在跨域无权限访问的问题,不管你是静态页面.动态网页.web服务,只要是跨域请求,都无法成功: 如果上句话没明白,我们直接看例子.有两个一模 ...
- #VSTS定制#全新的模版定制能力
在应用生命周期管理(ALM)领域中,存在各种不同的管理模型,如:传统的瀑布(waterfall)模型,CMMI模型以及最近一些年开始流行的敏捷模型,Scrum模型,kanban等等.每个不同的管理模型 ...
- 关于tempdb的一些注意事项
由于数据库的文件的位置对于I/O性能如此重要,以至于在创建主数据文件的文职时,需要考虑tempdb性能对系统性的影响,因为它是最动态的数据库,速度还需要最快. 组成:有主数据文件和日志文件组成.从sq ...
- x01.os.8: 加载内核
在 x01.os.7 中,借助 freedos,学习了保护模式.但操作系统必须完成引导:boot, 加载内核:loader,kernel,进而管理process,memory,file等. 引导比较简 ...
- x01.Game.CubeRun: 风一样的女子
1.题解 小孩学英语比较有意思,Monkey three => 猴三,风一样的女子 => 风 Girl.诸如此类不是重点,重点是一花一世界,一草一天堂.花花草草,纷纷扰扰.大千世界,当别具 ...
- Java 集合系列13之 WeakHashMap详细介绍(源码解析)和使用示例
概要 这一章,我们对WeakHashMap进行学习.我们先对WeakHashMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用WeakHashMap.第1部分 WeakHashMap介绍 ...
- js中的遍历foreach,$.each(),$().each()
<!DOCTYPE html> <html> <head> <meta charset='UTF-8'> <title></title ...
- arm汇编进入C函数分析,C函数压栈,出栈,传参,返回值
环境及代码介绍 环境和源码 由于有时候要透彻的理解C里面的一些细节问题,所有有必要看看汇编,首先这一切的开始就是从汇编代码进入C的main函数过程.这里不使用编译器自动生成的这部分汇编代码,因为编译器 ...
- AI (Adobe Illustrator)详细用法(三)
本文主要是介绍和色彩相关的用法. 一.路径外观设置 1.设置描边粗细 手动输入20px 下拉选择 鼠标选中数值,按向上或向下的箭头调整 在右边的描边菜单中修改 Note:按住shift键,然后上下箭头 ...