1.直接AXIS调用远程的webservice

注意:不同版本的Axis相差很大,大家最好以apache网站上的例子为准,这里仅仅用于说明其基本用法。

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class WebServiceClient {
public static void main(String[] args) {
try {
String endpoint = "http://172.19.0.153:8080/scs-web/webservice/SignService";
// 直接引用远程的wsdl文件
// 以下都是套路
Service service = new Service();
call.setOperationName("signContract");// WSDL里面描述的接口名称
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(endpoint);
call.addParameter("channel",org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN);// 接口的参数
call.addParameter("templateId",org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN);// 接口的参数
call.addParameter("strJson",org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN);// 接口的参数
call.addParameter("isSeal",org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN);// 接口的参数
call.addParameter("callBackUrl",org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN);// 接口的参数
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 设置返回类型
String templateId = "3021";
String channel = "1007";
String strJson = "{\"certApplys\":[{\"useridno\":\"N256613448988875\",\"address\":\"山东省德州临邑县恒源\",\"engName\":\"test\",\"telno\":\"15869611844\",\"email\":\"test@credithc.com\",\"username\":\"CHENG AI-HSIANG\",\"idTypeCode\":\"Z\"}],\"singnalElement\":{\"a\":\"\",\"b\":\"√\",\"product\":\"利投宝12_B\",\"amount\":\"100,000.00\",\"idType\":\"户照\",\"year\":2016,\"paybankName\":\"兴业银行\",\"backbankCardNo\":\"622908357966352914\",\"idNo\":\"N213447\",\"month\":12,\"lockTime\":12,\"paybankCardNo\":\"622908357966352914\",\"bigAmount\":\"壹拾万元整\",\"name\":\"CHENG AI-HSIANG\",\"customerId\":\"C_20161214000158\",\"contractId\":\"L_20161214291739\",\"backbankName\":\"兴业银行\",\"yearIrr\":\"9.6%\",\"payName\":\"CHENG AIHSIANG\",\"day\":14}}\r\n";
String isSeal = "1";
String callBackUrl = "?"; String result = (String) call.invoke(new Object[] {channel,templateId,isSeal,strJson,callBackUrl});
// 给方法传递参数,并且调用方法
System.out.println("result is :" + result);
} catch (Exception e) {
System.err.println(e.toString());
}
}
}

2.使用Http方式调用远程的webservice

package com.webservice;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection; public class WebServiceClient2 {
public static void main(String[] args) {
try {
// 1 指定WebService服务的请求地址:
String wsUrl = "http://172.19.0.153:8080/scs-web/webservice/SignService"; // 2 创建URL:
URL url = new URL(wsUrl);
// 3 建立连接,并将连接强转为Http连接
URLConnection conn = url.openConnection();
HttpURLConnection con = (HttpURLConnection) conn; // 4,设置请求方式和请求头:
con.setDoInput(true); // 是否有入参
con.setDoOutput(true); // 是否有出参
con.setRequestMethod("POST"); // 设置请求方式
con.setRequestProperty("content-type", "text/xml;charset=UTF-8"); // 5,手动构造请求体
String channel = "1007";
String templateId = "3021";
String isSeal = "1";
String strJson = "{\"certApplys\":[{\"useridno\":\"N256613448988875\",\"address\":\"山东省德州临邑县恒源\",\"engName\":\"test\",\"telno\":\"15869611844\",\"email\":\"test@credithc.com\",\"username\":\"CHENG AI-HSIANG\",\"idTypeCode\":\"Z\"}],\"singnalElement\":{\"a\":\"\",\"b\":\"√\",\"product\":\"利投宝12_B\",\"amount\":\"100,000.00\",\"idType\":\"户照\",\"year\":2016,\"paybankName\":\"兴业银行\",\"backbankCardNo\":\"622908357966352914\",\"idNo\":\"N213447\",\"month\":12,\"lockTime\":12,\"paybankCardNo\":\"622908357966352914\",\"bigAmount\":\"壹拾万元整\",\"name\":\"CHENG AI-HSIANG\",\"customerId\":\"C_20161214000158\",\"contractId\":\"L_20161214291739\",\"backbankName\":\"兴业银行\",\"yearIrr\":\"9.6%\",\"payName\":\"CHENG AIHSIANG\",\"day\":14}}\r\n";
String callBackUrl = "?";   String  requestBody = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"";
requestBody += " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"";
requestBody += " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
requestBody += "<soapenv:Body>";
requestBody += "<q0:signContract xmlns:q0=\"http://impl.webservice.scs.credithc.com/\">";
requestBody += "<channel>" + channel + "</channel>";
requestBody += "<templateId>" + templateId + "</templateId> ";
requestBody += "<isSeal>" + isSeal + "</isSeal> ";
requestBody += "<strJson>" + strJson + "</strJson> ";
requestBody += "<callBackUrl>" + callBackUrl + "</callBackUrl> ";
requestBody += "</q0:signContract>";
requestBody += "</soapenv:Body>";   requestBody += "</soapenv:Envelope>"; // 6,通过流的方式将请求体发送出去:
OutputStream out = con.getOutputStream();
out.write(requestBody.getBytes());
out.close();
// 7,服务端返回正常:
int code = con.getResponseCode();
if (code == 200) {// 服务端返回正常
InputStream is = con.getInputStream();
byte[] b = new byte[1024];
StringBuffer sb = new StringBuffer();
int len = 0;
while ((len = is.read(b)) != -1) {
String str = new String(b, 0, len, "UTF-8");
sb.append(str);
}
System.out.println(sb.toString());
    is.close();
}
con.disconnect();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}https://blog.csdn.net/java_cainiao2016/article/details/80032377

3.springboot 动态调用

package com.credithc.re.sign.webservice;

import com.credithc.re.sign.service.RedisService;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.namespace.QName;
import java.util.UUID; @WebService(name = "reSignService")
@Component
public class ReSignService { @Autowired
private RedisService redisService; @WebMethod
@WebResult
public String reSign(@WebParam(name = "channel") String channel,
@WebParam(name = "templateId") String templateId,
@WebParam(name = "isSeal") int isSeal,
@WebParam(name = "strJson") String strJson,
@WebParam(name = "callBackUrl") String callBackUrl) {
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
redisService.set(uuid, callBackUrl);
callBackUrl = "http://172.19.0.153:8081/re/sign/callback" + "?id=" + uuid; // 创建动态客户端
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://172.19.0.153:8080/scs-web/webservice/SignService?wsdl");
Object[] objects = new Object[0];
try {
QName opName = new QName("http://webservice.scs.credithc.com/", "signContract");
objects = client.invoke(opName, channel, templateId, isSeal, strJson, callBackUrl);
System.out.println("返回数据:" + objects[0]);
} catch (java.lang.Exception e) {
e.printStackTrace();
}
return "请求成功";
}
}
package com.credithc.re.sign.config;

import com.credithc.re.sign.webservice.ReSignService;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import javax.xml.ws.Endpoint; @Configuration
public class CxfConfig { @Autowired
private Bus bus; @Autowired
private ReSignService reSignService; @Bean
public Endpoint endpoint(){
EndpointImpl endpoint = new EndpointImpl(bus,reSignService);
endpoint.publish("/reSignService");
return endpoint;
}
}

java调用webservice接口的更多相关文章

  1. Java调用webservice接口方法

                             java调用webservice接口   webservice的 发布一般都是使用WSDL(web service descriptive langu ...

  2. java 调用webservice接口wsdl,推荐使用wsdl2java,放弃wsimport

    网上说wsimport是jdk1.6后自带的客户端生成调用webservice接口的工具,其实我挺喜欢原生的东西,毕竟自家的东西用着应该最顺手啊,但往往让人惊艳的是那些集成工具. 本机jdk1.8.1 ...

  3. Java调用webservice接口方法(SOAP message、xfire、axis)

    webservice的 发布一般都是使用WSDL(web service descriptive language)文件的样式来发布的,在WSDL文件里面,包含这个webservice暴露在外面可供使 ...

  4. java 调用webservice接口

    webservice的 发布一般都是使用WSDL(web service descriptive language)文件的样式来发布的,在WSDL文件里面,包含这个webservice暴露在外面可供使 ...

  5. Java调用WebService接口实现发送手机短信验证码功能,java 手机验证码,WebService接口调用

    近来由于项目需要,需要用到手机短信验证码的功能,其中最主要的是用到了第三方提供的短信平台接口WebService客户端接口,下面我把我在项目中用到的记录一下,以便给大家提供个思路,由于本人的文采有限, ...

  6. java调用webservice接口 几种方法

    webservice的 发布一般都是使用WSDL(web service descriptive language)文件的样式来发布的,在WSDL文件里面,包含这个webservice暴露在外面可供使 ...

  7. JAVA调用WebService接口(以调用第三方天气接口为例)

    天气接口地址:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl 1.打开项目文件目录路径文件夹,在当前文件夹打开cmd, ...

  8. Java之HttpClient调用WebService接口发送短信源码实战

    摘要 Java之HttpClient调用WebService接口发送短信源码实战 一:接口文档 二:WSDL 三:HttpClient方法 HttpClient方法一 HttpClient方法二 Ht ...

  9. java如何调用webservice接口

    java调用WebService可以直接使用Apache提供的axis.jar自己编写代码,或者利用Eclipse自动生成WebService Client代码,利用其中的Proxy类进行调用.理论上 ...

  10. java 调用webservice的各种方法总结

    java 调用webservice的各种方法总结 几种流行的开源WebService框架Axis1,Axis2,Xfire,CXF,JWS比较 方法一:创建基于JAX-WS的webservice(包括 ...

随机推荐

  1. #交互,鸽笼原理#CF1776C Library game

    题目 有一个长度为 \(m\) 的书架,以及 \(n\) 个长度 \(a_1,a_2,\dots,a_n\) Alessia 和 Bernardo 从书架上取书.每次由 Alessia 选择一个之前没 ...

  2. 关于pwn题的栈平衡中ret的作用

    以nssctf里的where_is_my_shell为例 题目提供了一个system函数,和一个buf数组.数组的栈空间如图所示,这里不讨论怎么解题,只说明payload里的ret的作用. 假设没有r ...

  3. Jetty的http模块

    启用http模块,执行如下命令: java -jar $JETTY_HOME/start.jar --add-modules=http 查看http模块的配置文件,执行如下命令: cat $JETTY ...

  4. 使用windbg分析dump文件

    使用windbg分析dump文件的步骤. 准备工作. 打开dump文件. 指定符号表文件的路径. 指定可执行文件的路径. 指定源码文件的路径. 在windbg的命令行,输入并执行如下命令 .reloa ...

  5. OpenHarmony社区运营报告(2022年10月)

    本月快讯 ● <深圳市推动软件产业高质量发展的若干措施>于10月24日发布. ● 社区共发展逾5000位贡献者累计为社区提交超过11万个PR,深圳市优博终端科技有限公司(以下简称" ...

  6. Docker学习路线10:容器安全

    容器安全是实施和管理像Docker这样的容器技术的关键方面.它包括一组实践.工具和技术,旨在保护容器化应用程序及其运行的基础架构.在本节中,我们将讨论一些关键的容器安全考虑因素.最佳实践和建议. 容器 ...

  7. 掌握 xUnit 单元测试中的 Mock 与 Stub 实战

    引言 上一章节介绍了 TDD 的三大法则,今天我们讲一下在单元测试中模拟对象的使用. Fake Fake - Fake 是一个通用术语,可用于描述 stub或 mock 对象. 它是 stub 还是 ...

  8. redis 简单整理——缓存设计[三十二]

    前言 简单整理一下缓存设计. 正文 缓存的好处: ·加速读写:因为缓存通常都是全内存的(例如Redis.Memcache),而 存储层通常读写性能不够强悍(例如MySQL),通过缓存的使用可以有效 地 ...

  9. 重新整理数据结构与算法(c#)—— 堆排序[二十一]

    前言 将下面按照从小到大排序: int[] arr = { 4, 6, 8, 5, 9 }; 这时候可以通过冒泡排序,计数排序等. 但是一但数据arr很大,那么会产生排序过于缓慢,堆排序就是一个很好的 ...

  10. 力扣13(java)-罗马数字转整数(简单)

    题目: 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值I 1V 5X 10L 50C 100D 500M 1000例如, 罗马数字 2 写做 II ,即为两个并列的 1 ...