java调用.net asmx / wcf

简介: 一、先用asmx与wcf写二个.net web service: 1.1 asmx web服务:asmx-service.asmx.cs 1 using System; 2 using System.

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

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

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Services;
 6 
 7 namespace WebServiceSample
 8 {
 9     /// <summary>
10     /// Summary description for asmx_service
11     /// </summary>
12     [WebService(Namespace = "http://yjmyzz.cnblogs.com/")]
13     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
14     [System.ComponentModel.ToolboxItem(false)]
15     // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
16     // [System.Web.Script.Services.ScriptService]
17     public class asmx_service : System.Web.Services.WebService
18     {
19 
20         [WebMethod]
21         public string HelloWorld(String msg)
22         {
23             return "Hello " + msg + " !";
24         }
25     }
26 }
View Code

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

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Runtime.Serialization;
 5 using System.ServiceModel;
 6 using System.Text;
 7 using System.Web.Services;
 8 
 9 namespace WebServiceSample
10 {
11     // 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.
12     // 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.
13     [ServiceContract(Namespace="http://yjmyzz.cnblogs.com/")]
14     [ServiceBehavior(Namespace = "http://yjmyzz.cnblogs.com/")]
15     public class wcf_service
16     {
17         [OperationContract]
18         public String HelloWorld(String msg)
19         {
20             return "Hello " + msg + " !";
21         }
22     }
23 }
View Code

1.3 web.config采用默认设置:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <configuration>
 3   <system.web>
 4     <compilation debug="true" targetFramework="4.5.1" />
 5     <httpRuntime targetFramework="4.5.1" />
 6   </system.web>
 7   <system.serviceModel>
 8     <behaviors>
 9       <serviceBehaviors>
10         <behavior name="">
11           <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
12           <serviceDebug includeExceptionDetailInFaults="false" />
13         </behavior>
14       </serviceBehaviors>
15     </behaviors>
16     <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
17       multipleSiteBindingsEnabled="true" />
18   </system.serviceModel>
19 </configuration>
View Code

完成后,访问网址为:

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

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

 

二、java端的调用:

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

 1 <dependency>
 2             <groupId>org.apache.axis</groupId>
 3             <artifactId>axis</artifactId>
 4             <version>1.4</version>            
 5         </dependency>
 6 
 7         <dependency>
 8             <groupId>org.apache.axis</groupId>
 9             <artifactId>axis-jaxrpc</artifactId>
10             <version>1.4</version>            
11         </dependency>
12 
13         <dependency>
14             <groupId>wsdl4j</groupId>
15             <artifactId>wsdl4j</artifactId>
16             <version>1.6.3</version>            
17         </dependency>
18 
19         <dependency>
20             <groupId>commons-discovery</groupId>
21             <artifactId>commons-discovery</artifactId>
22             <version>0.5</version>            
23         </dependency>
24 
25         <dependency>
26             <groupId>commons-logging</groupId>
27             <artifactId>commons-logging</artifactId>
28             <version>1.1.3</version>            
29         </dependency>
View Code

2.2 asmx web service的调用:

先封装一个方法:

 1     String callAsmxWebService(String serviceUrl, String serviceNamespace,
 2             String methodName, Map<String, String> params)
 3             throws ServiceException, RemoteException, MalformedURLException {
 4 
 5         org.apache.axis.client.Service service = new org.apache.axis.client.Service();
 6         Call call = (Call) service.createCall();
 7         call.setTargetEndpointAddress(new java.net.URL(serviceUrl));
 8         call.setOperationName(new QName(serviceNamespace, methodName));
 9 
10         ArrayList<String> paramValues = new ArrayList<String>();
11         for (Entry<String, String> entry : params.entrySet()) {
12             call.addParameter(new QName(serviceNamespace, entry.getKey()),
13                     XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
14             paramValues.add(entry.getValue());
15         }
16 
17         call.setReturnType(XMLType.XSD_STRING);
18         call.setUseSOAPAction(true);
19         call.setSOAPActionURI(serviceNamespace + methodName);
20 
21         return (String) call.invoke(new Object[] { paramValues.toArray() });
22 
23     }
View Code

然后就可以调用了:

 1     @Test
 2     public void testCallAsmx() throws RemoteException, ServiceException,
 3             MalformedURLException {        
 4 
 5         Map<String, String> params = new HashMap<String, String>();
 6         params.put("msg", "yjmyzz");
 7 
 8         String result = callAsmxWebService(
 9                 "http://localhost:16638/asmx-service.asmx",
10                 "http://yjmyzz.cnblogs.com/", "HelloWorld", params);
11 
12         System.out.println(result);
13     }
View Code

 

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文件:

然后就能调用啦:

1     @Test
2     public void testCallWcf() throws RemoteException, ServiceException,
3             MalformedURLException {        
4         
5         Wcf_service_ServiceLocator locator = new Wcf_service_ServiceLocator();
6         locator.setBasicHttpBinding_wcf_serviceEndpointAddress("http://localhost:16638/wcf-service.svc");
7         System.out.println(locator.getBasicHttpBinding_wcf_service().helloWorld("jimmy"));         
8     }
View Code

 

目录
相关文章
|
6月前
|
数据采集 自然语言处理 Java
Playwright 多语言一体化——Python/Java/.NET 全栈采集实战
本文以反面教材形式,剖析了在使用 Playwright 爬取懂车帝车友圈问答数据时常见的配置错误(如未设置代理、Cookie 和 User-Agent),并提供了 Python、Java 和 .NET 三种语言的修复代码示例。通过错误示例 → 问题剖析 → 修复过程 → 总结教训的完整流程,帮助读者掌握如何正确配置爬虫代理及其它必要参数,避免 IP 封禁和反爬检测,实现高效数据采集与分析。
391 3
Playwright 多语言一体化——Python/Java/.NET 全栈采集实战
|
7月前
|
数据采集 自然语言处理 JavaScript
Playwright多语言生态:跨Python/Java/.NET的统一采集方案
随着数据采集需求的增加,传统爬虫工具如Selenium、Jsoup等因语言割裂、JS渲染困难及代理兼容性差等问题,难以满足现代网站抓取需求。微软推出的Playwright框架,凭借多语言支持(Python/Java/.NET/Node.js)、统一API接口和优异的JS兼容性,解决了跨语言协作、动态页面解析和身份伪装等痛点。其性能优于Selenium与Puppeteer,在学术数据库(如Scopus)抓取中表现出色。行业应用广泛,涵盖高校科研、大型数据公司及AI初创团队,助力构建高效稳定的爬虫系统。
423 2
Playwright多语言生态:跨Python/Java/.NET的统一采集方案
|
11月前
|
开发框架 监控 .NET
C#进阶-ASP.NET WebForms调用ASMX的WebService接口
通过本文的介绍,希望您能深入理解并掌握ASP.NET WebForms中调用ASMX WebService接口的方法和技巧,并在实际项目中灵活运用这些技术,提高开发效率和应用性能。
701 5
|
Java 网络安全 Maven
Exception in thread "main" java.lang.NoSuchMethodError: okhttp3.OkHttpClient$Builder.sslSocketFactory(Ljavax/net/ssl/SSLSocketFactory;Ljavax/net/ssl/X509TrustManager;)Lokhttp3/OkHttpClient$Builder; 问题处理
【10月更文挑战第26天】Exception in thread "main" java.lang.NoSuchMethodError: okhttp3.OkHttpClient$Builder.sslSocketFactory(Ljavax/net/ssl/SSLSocketFactory;Ljavax/net/ssl/X509TrustManager;)Lokhttp3/OkHttpClient$Builder; 问题处理
540 2
|
算法 Java 测试技术
java 访问ingress https报错javax.net.ssl.SSLHandshakeException: Received fatal alert: protocol_version
java 访问ingress https报错javax.net.ssl.SSLHandshakeException: Received fatal alert: protocol_version
1954 1
|
Java 开发工具 Spring
【Azure Spring Cloud】使用azure-spring-boot-starter-storage来上传文件报错: java.net.UnknownHostException: xxxxxxxx.blob.core.windows.net: Name or service not known
【Azure Spring Cloud】使用azure-spring-boot-starter-storage来上传文件报错: java.net.UnknownHostException: xxxxxxxx.blob.core.windows.net: Name or service not known
174 0
|
开发框架 安全 Java
.net和java有什么样的区别?
Java和.NET在本质、编程语言、生态系统与工具、跨平台性、应用领域、性能与效率以及安全性与可靠性等方面都存在明显的区别。选择哪个平台取决于具体的需求、技术栈和目标平台。
986 7
|
网络协议 Java 测试技术
【Java】已解决java.net.BindException异常
【Java】已解决java.net.BindException异常
912 0
|
Java 网络安全 网络架构
【Java】已解决java.net.ConnectException异常
【Java】已解决java.net.ConnectException异常
3213 0
|
Java
【Java】已解决java.net.MalformedURLException异常
【Java】已解决java.net.MalformedURLException异常
1445 0

热门文章

最新文章

下一篇
oss云网关配置