1、jdk原生调用(需要获取服务接口文件)

import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service; import com.soft.platform.webservice.server.MyService; public class WsClient { public static void main(String[] args) throws Exception {
URL url = new URL("http://192.168.0.101:8089/myservice?wsdl");
// 指定命名空间和服务名称
QName qName = new QName("http://com.soft.ws/my", "MyService");
Service service = Service.create(url, qName);
// 通过getPort方法返回指定接口
MyService myServer = service.getPort(new QName("http://com.soft.ws/my",
"LoginPort"), MyService.class);
// 调用方法 获取返回值
String result = myServer.authorization("admin", "123456");
System.out.println(result);
} }
返回结果: success

2.import生成客户端代码
wsimport -d d:/webservice -keep -p com.soft.test.wsimportClient -verbose http://192.168.0.101:8089/myservice?wsdl

public static void main(String[] args) {
MyService_Service service = new MyService_Service();
MyService login = service.getLoginPort();
String result = login.authorization("admin", "123456");
System.out.println(result);
}

3、cxf两种调用方式。

public static void main(String[] args) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(MyService.class);
factory.setAddress("http://192.168.0.101:8089/myservice?wsdl");
// 需要服务接口文件
MyService client = (MyService) factory.create();
String result = client.authorization("admin", "123456");
System.out.println(result);
}

  

public static void main(String[] args) throws Exception {
//采用动态工厂方式 不需要指定服务接口
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf
.createClient("http://192.168.0.101:8089/myservice?wsdl");
QName qName = new QName("http://com.soft.ws/my", "authorization");
Object[] result = client.invoke(qName,
new Object[] { "admin", "123456" });
System.out.println(result[0]);
}

4、axis调用方式

import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException; import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException; import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType; public class WsAClient {
/**
* 跨平台调用Web Service出现
* faultString: 服务器未能识别 HTTP 头 SOAPAction 的值:
* JAX-WS规范不需要SoapAction,但是.NET需要,所以产生了这个错误。
* options.setAction("目标的TargetNameSpace"+"调用的方法名");
*/
public static void main(String[] args) {
String url = "http://192.168.0.101:8089/myservice?wsdl";
Service service = new Service();
try {
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new URL(url));
// WSDL里面描述的接口名称(要调用的方法)
call.setOperationName(new QName("http://com.soft.ws/my",
"authorization"));
//跨平台调用加上这个
call.setUseSOAPAction(true);
call.setSOAPActionURI("http://com.soft.ws/my/authorization");
// 接口方法的参数名, 参数类型,参数模式 IN(输入), OUT(输出) or INOUT(输入输出)
call.addParameter("userId", XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter("password", XMLType.XSD_STRING, ParameterMode.IN);
// 设置被调用方法的返回值类型
call.setReturnType(XMLType.XSD_STRING);
// 设置方法中参数的值
Object result = call.invoke(new Object[] { "admin", "123456" }); System.out.println(result.toString());
} catch (ServiceException | RemoteException | MalformedURLException e) {
e.printStackTrace();
}
} }

axis方式依赖的相关jar包如下:

5、httpClient调用方式。

(1)maven依赖如下

<properties>
<httpclient.version>4.5.6</httpclient.version>
</properties> <dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
</dependencies>

(2)httpclient作为客户端调用webservice。代码如下

/*
* Copyright (c)
*/
package test; import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; import java.nio.charset.Charset; /**
* webservice客户端
*
* @author David Lin
* @version: 1.0
* @date 2018-09-09 12:16
*/
public class SoapClient { public static void main(String args[]) throws Exception {
//soap服务地址
String url = "http://localhost:8888/ssm/Services/UserService?wsdl";
StringBuilder soapBuilder = new StringBuilder(64);
soapBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
soapBuilder.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservice.soft.com/\">");
soapBuilder.append(" <soapenv:Header/>");
soapBuilder.append(" <soapenv:Body>");
soapBuilder.append(" <web:authorization>");
soapBuilder.append(" <userId>").append("admin").append("</userId>");
soapBuilder.append(" <password>").append("123456").append("</password>");
soapBuilder.append(" </web:authorization>");
soapBuilder.append(" </soapenv:Body>");
soapBuilder.append("</soapenv:Envelope>"); //创建httpcleint对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建http Post请求
HttpPost httpPost = new HttpPost(url);
// 构建请求配置信息
RequestConfig config = RequestConfig.custom().setConnectTimeout(1000) // 创建连接的最长时间
.setConnectionRequestTimeout(500) // 从连接池中获取到连接的最长时间
.setSocketTimeout(3 * 1000) // 数据传输的最长时间10s
.build();
httpPost.setConfig(config);
CloseableHttpResponse response = null;
try {
//采用SOAP1.1调用服务端,这种方式能调用服务端为soap1.1和soap1.2的服务
httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8"); //采用SOAP1.2调用服务端,这种方式只能调用服务端为soap1.2的服务
// httpPost.setHeader("Content-Type", "application/soap+xml;charset=UTF-8");
StringEntity stringEntity = new StringEntity(soapBuilder.toString(), Charset.forName("UTF-8"));
httpPost.setEntity(stringEntity);
response = httpClient.execute(httpPost);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(content); } else {
System.out.println("调用失败!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != response) {
response.close();
}
if (null != httpClient) {
httpClient.close();
}
} }
}

返回结果为:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:authorizationResponse xmlns:ns2="http://webservice.soft.com/">
<return>success</return>
</ns2:authorizationResponse>
</soap:Body>
</soap:Envelope>

(3)用Jsoup提取响应数据。
maven依赖

<properties>
<jsoup.version>1.11.3</jsoup.version>
</properties> <dependencies> <dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>${jsoup.version}</version>
</dependency> </dependencies>

代码如下:

 Document soapRes = Jsoup.parse(content);
Elements returnEle = soapRes.getElementsByTag("return"); System.out.println("调用结果为:"+returnEle.text());

WebService客户端几种实现方式的更多相关文章

  1. WebService的几种验证方式

    转 http://www.cnblogs.com/yoshiki1895/archive/2009/06/03/1495440.html WebService的几种验证方式 1.1      WebS ...

  2. WebService学习整理(一)——客户端三种调用方式整理

    1 WebService基础 1.1 作用 1,       WebService是两个系统的远程调用,使两个系统进行数据交互,如应用: 天气预报服务.银行ATM取款.使用邮箱账号登录各网站等. 2, ...

  3. java 调用wsdl的webservice接口 两种调用方式

    关于wsdl接口对于我来说是比较头疼的 基本没搞过.一脸懵 就在网上搜 看着写的都很好到我这就不好使了,非常蓝瘦.谨以此随笔纪念我这半个月踩过的坑... 背景:短短两周除了普通开发外我就接到了两个we ...

  4. webservice的两种调用方式

    如下 using ConsoleApplication1.TestWebService; using System; using System.Collections; using System.Co ...

  5. C#调用webService的几种方法

    转自: WebClient 用法小结 http://www.cnblogs.com/hfliyi/archive/2012/08/21/2649892.html http://www.cnblogs. ...

  6. Axis2开发WebService客户端 的3种方式

    Axis2开发WebService客户端 的3种方式 在dos命令下   wsdl2java        -uri    wsdl的地址(网络上或者本地)   -p  com.whir.ezoffi ...

  7. WebService的两种方式Soap和Rest比较

    我的读后感:由于第一次接触WebService,对于很多概念不太理解,尤其是看到各个OpenAPI的不同提供方式时,更加疑惑.如google map api采用了AJAX方式,通过javascript ...

  8. C# .NET 动态调用webservice的三种方式

    转载自 百度文库 http://wenku.baidu.com/link?url=Q2q50wohf5W6UX44zqotXFEe_XOMaib4UtI3BigaNwipOHKNETloMF4ax4W ...

  9. WebService的两种方式SOAP和REST比较 (转)

    我的读后感:由于第一次接触WebService,对于很多概念不太理解,尤其是看到各个OpenAPI的不同提供方式时,更加疑惑.如google map api采用了AJAX方式,通过javascript ...

随机推荐

  1. 第四章 Spring.Net 如何管理您的类___统一资源访问接口

    在前面章节有童鞋提到过 关于配置文件 Objects.xml 路径的相关问题,这些东西是 IResource 接口的一些内容,接下来就详细介绍一下 IResource 接口. IResource 接口 ...

  2. iis部署网站,使用虚拟路劲

    此前一直使用vs2010,没有考虑过配置IIS,但是一个项目完成后交付给甲方使用.肯定是要考虑IIS的安装和部署的.现从IIS的安装和asp.NET项目的部署两个方面讲解. IIS安装: 网上很多教程 ...

  3. 高级类特性----static关键字

    static 关键字 当我们编写一个类时,其实就是在描述其对象的属性和行为,而并没有产生实质上的对象,只有通过new关键字才会产生出对象,这时系统才会分配内存空间给对象,其方法才可以供外部调用. 我们 ...

  4. hex()

    hex() 用于将十进制数字转换成十六进制 In [1]: hex(10) Out[1]: '0xa' In [2]: hex(11) Out[2]: '0xb'

  5. php检测文件只读、可写、可执行权限

    例子:检测文件是否可读.可写.可执行. 复制代码代码示例: <?php  $myfile = "./test.txt"; if (is_readable ($myfile)) ...

  6. UITableView取消选中颜色、常用操作

    UITableView取消选中颜色.常用操作   使用空白view取代cell - (UITableViewCell *)tableView:(UITableView *)tableView cell ...

  7. Swift-属性、方法、下标

    存储属性和计算属性 类.结构和枚举都能够定义存储属性和计算属性.其中存储属性就是常见的形式,又分为变量属性和常量属性,如: struct Point { var x = 0.0, y = 0.0 } ...

  8. poj_2752 kmp

    题目大意 给定字符串S,求出S的所有可能相同前后缀的长度.比如: "alala"的前缀分别为{"a", "al", "ala&qu ...

  9. 【PHP】算法: 获取满足给定值的最优组合

    PHP 获取给定值的最优组合 方法 -   (蓝洛提供,博客地址: www.zhaorui.info) <?php ini_set('error_reporting','E_ALL^E_NOTI ...

  10. LeetCode——Lowest Common Ancestor of a Binary Search Tree

    Description: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given no ...