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 ...
随机推荐
- stringstream操纵string小总结
1 split字符串 之前在用C#写代码的时候,用过split函数,可以把一个字符串根据某个分隔符分成若干个字符串数组.在用C++操纵字符串的时候,我一直使用很笨的遍历的方法.为此,我问候过很多次C+ ...
- Mongodb源代码阅读笔记:Journal机制
Mongodb源代码阅读笔记:Journal机制 Mongodb源代码阅读笔记:Journal机制 涉及的文件 一些说明 PREPLOGBUFFER WRITETOJOURNAL WRITETODAT ...
- memcache使用方法测试 # 转自 简单--生活 #
<?php //php操作memcache的使用测试总结--学习 //1 Memcache::connect; //$memcache = new Memcache; //$ ...
- Access restriction: The type 'RSACipher' is not API
解决方法: http://stackoverflow.com/questions/860187/access-restriction-on-class-due-to-restriction-on-re ...
- Linux下的压缩zip,解压缩unzip命令详解及实例
实例:压缩服务器上当前目录的内容为xxx.zip文件 zip -r xxx.zip ./* 解压zip文件到当前目录 unzip filename.zip ====================== ...
- Java文件选择对话框(文件选择器JFileChooser)的使用:以一个文件加密器为例
文件加密器,操作过程肯定涉及到文件选择器的使用,所以这里以文件加密器为例.下例为我自己写的一个文件加密器,没什么特别的加密算法,只为演示文件选择器JFileChooser的使用. 加密器界面如图: 项 ...
- KEIL MDK STM32如何建立工程
2. 3 4 5 6 7 QQ 463431476 8 9
- 简易的IOS位置定位服务
有时一些小的需求,其实只是需要得知当前IOS APP使用的地点,有些只是想精确到城市级别,并不需要任何地图. 有了以下的简易实现: @interface MainViewController ()&l ...
- 【工作常用代码集】批量Telnet远端端口
作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/ __author__ = 'gnuhpc' import telnetlib,socket IP={} def ...
- 《100种过度医疗大公开》:转译自日文版,日文版依据的是美国的“Choosing Wisely”项目。三星推荐
本书转译自日文,日文版则是在美国的“Choosing Wisely”项目中选择了100个相对常见的过度医疗项目做解说.Choosing Wisely项目,是由美国多个专业医学组织发起的列出过度医疗项目 ...