一、先用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的更多相关文章

  1. Java调用Webservice(asmx)的几个例子

    Java调用Webservice(asmx)的几个例子 2009-06-28 17:07 写了几个调用例子: 1. import org.apache.axis.client.*;import org ...

  2. 用java调用.net的wcf其实还是很简单的

      前些天和我们的一个邮件服务商对接,双方需要进行一些通讯,对方是java团队,而作为.net团队的我们,只能公布出去的是一个wcf的basicbinding,想不 到问题来了,对方不知道怎么去调用这 ...

  3. PHP&Java 调用C#的WCF

    步骤一:用C#声明WCF [ServiceContract] public interface IService1 { [OperationContract] void DoWork(); [Oper ...

  4. java调用.net asmx服务

    有时候,在java下开发会调用一下.net下写的服务,看网上有各种方法,但总是不成功,总结下自己测试过能调用成功的方式: 1. 原始方式http-soap public static String p ...

  5. java 调用webservice (asmx) 客户端开发示例

    这是本人第一次写博客,其实就是自己做个笔记,写的很粗糙,也希望能给跟我遇到同样问题的你一点帮助. 因为最近有个项目要调用webservice接口,之前接触的都是Java开发服务端和客户端的接口,开发前 ...

  6. JAVA调用WCF

    Java环境下生成代理类的工具有很多,如wsdl2Java,wsimport 等.本文中使用的工具是wsimport. 1.wsdl2Java 生成命令实例: wsdl2Java -p package ...

  7. Web循环监控Java调用 / Java调用.net wcf接口

    背景介紹 (Background Introduction) 目前有一些报表客户抱怨打不开 报表执行过程过长,5.8.10秒甚至更多 不能及时发现和掌握服务器web站点情况 用戶需求(User Req ...

  8. 【转】java调用webservice

    互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的Web应用中显示,下面就以获取天气预报数据和查询国内手机号码归属地为 ...

  9. 分享:根据webservice WSDL地址自动生成java调用代码及JAR包

    分享:根据webservice WSDL地址自动生成java调用代码及JAR包使用步骤:一.安装java 并配置JAVA_HOME 及 path二.安装ANT 并配置ANT_HOME三.解压WsdlT ...

随机推荐

  1. 【转】简析 .NET Core 构成体系

    前文介绍了.NET Core 在整个.NET 平台所处的地位,以及与.NET Framework的关系(原文链接),本文将详细介绍.NET Core 框架的构成和各模块主要功能,以及如何实现跨平台. ...

  2. Javascript 优化项目代码技巧之语言基础(二)

        上一篇随笔介绍了如何正确判断对象类型.避免变量污染,特殊值(null.undefined.NaN)的使用,以及其他Javascript中常用关键字与方法的优化,这篇随笔将着重介绍Javascr ...

  3. visual studio生成后调试启动又提示部分项目需要生成问题总结

    长久以来若干个项目都遇到过类似的情形,已经成功生成的项目启动调试或者再生成依然认为部分项目需要生成而不是跳过.总结以往的经验,记录下来,以便大家遇到时处理. 若有多个项目提示需要重新生成,优先检查被依 ...

  4. Linux账户密码过期安全策略设置

    在Linux系统管理中,有时候需要设置账号密码复杂度(长度).密码过期策略等,这个主要是由/etc/login.defs参数文件中的一些参数控制的的.它主要用于用户账号限制,里面的参数主要有下面一些: ...

  5. mysql技巧之select count的比较

        在工作过程中,时不时会有开发咨询几种select count()的区别,我总会告诉他们使用select count(*) 就好.下文我会展示几种sql的执行计划来说明为啥是这样.   1.测试 ...

  6. java JedisUtils工具类

    package com.sh.xrsite.common.utils; import java.util.List; import java.util.Map; import java.util.Se ...

  7. 大数据挖掘: FPGrowth初识--进行商品关联规则挖掘

    @(hadoop)[Spark, MLlib, 数据挖掘, 关联规则, 算法] [TOC] 〇.简介 经典的关联规则挖掘算法包括Apriori算法和FP-growth算法.Apriori算法多次扫描交 ...

  8. ReactNative之坑爹的在线安装

    编译一个github上ReactNative应用,根据说明只有3步: npm installreact-native run-androidenjoy 但几个步骤实在是一波三折充满着坎坷,一点都不en ...

  9. POJ2184 Cow Exhibition[DP 状态负值]

    Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12420   Accepted: 4964 D ...

  10. UWP数据绑定

    [ 已针对 Windows 10 上的 UWP 应用更新. 有关 Windows 8.x 文章,请参阅存档 ] 数据绑定是你的应用 UI 用来显示数据的一种方法,可以选择与该数据保持同步. 借助数据绑 ...