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 ...
随机推荐
- [Java]Hessian客户端和服务端代码例子
简要说明:这是一个比较简单的hessian客户端和服务端,主要实现从客户端发送指定的数据量到服务端,然后服务端在将接收到的数据原封不动返回到客户端.设计该hessian客户端和服务端的初衷是为了做一个 ...
- sql语句 在字段前面加0
目前数字如下: 1 2 3 10 想变成长度为5位,如果不够,前面补0, 写法如下 select right('00000'+ltrim(你的字段),5) 结果: 00001 00002 00003 ...
- Java并发之BlockingQueue 阻塞队列(ArrayBlockingQueue、LinkedBlockingQueue、DelayQueue、PriorityBlockingQueue、SynchronousQueue)
package com.thread.test.thread; import java.util.Random; import java.util.concurrent.*; /** * Create ...
- 首先,编写一个类ChongZai,该类中有3个重载的方法void print();其次, 再编写一个主类来测试ChongZai类的功能
//计算器 jisuanqi jsq=new jisuanqi(); System., )); System., , )); System.out.println("2.3和4.5 中最大的 ...
- python-1 python基础知识
python第一课代码笔记 hello world [root@heartbeat-data- python]# vim hello1.py #!/usr/bin/env python print ( ...
- java:comp/env/jdbc/ 的两种配置方法
1. 在 META-INF 下建立文件: context.xml <?xml version="1.0" encoding="UTF-8"?> &l ...
- Linux mke2fs 硬盘格式化
[root@whp6 ~]# cat /etc/filesystems ext4 ext3 ext2 nodev proc nodev devpts iso9660 vfat hfs hfsplus ...
- Windows Azure 负载均衡会话保持
Windows Azure的负载均衡器默认是5元组的hash:源地址,源端口,目的地址,目的端口,协议.即:只有上述五个元组完全一致的会话数据包才会被转发到同一个后端服务器.显然,对于绝大多数通过NA ...
- Jquery:小知识;
Jquery:小知识: jQuery学习笔记(二):this相关问题及选择器 上一节的遗留问题,关于this的相关问题,先来解决一下. this的相关问题 this指代的是什么 这个应该是比较好理 ...
- JAVA代码中加了Try...Catch的执行顺序
public static String getString(){ try { //return "a" + 1/0; return "a"; } catch ...