cxf 和 httpclient 客户端调用 webservice 接口
一、cxf 生成 webservice 客户端
1、接口路径 http://ws.webxml.com.cn/WebServices/WeatherWS.asmx
2、进入你需要放置 webservice 客户端代码的包,进入这个包所在的系统路径,进入 cmd
3、执行命令 wsimport -keep http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl 或者 wsdl2java -client http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl
3.1、如果报错信息如下:具有相同名称“xxx”的类/接口已经使用。
wsdl2java -client http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl
改为
wsdl2java -client -autoNameResolution http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl
4、spring 整合 cxf
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:client id="userClient"
serviceClass="com.java.webservice.service.impl.ITianQi" <!--生成的接口-->
address="http://ws.webxml.com.cn/WebServices/WeatherWS.asmx">
</jaxws:client>
</beans>
二、httpclient 调用 webservice
public static String getXML() {
StringBuffer sb = new StringBuffer();
sb.append("<?xml version='1.0' encoding='utf-8'?>"
+ "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>"
+ "<soap:Body>" + "<getMobileCodeInfo xmlns='http://WebXml.com.cn/'>"
+ "<mobileCode>string</mobileCode>" + " <userID>string</userID>" + "</getMobileCodeInfo>"
+ "</soap:Body>" + "</soap:Envelope>");
return sb.toString();
}
static int socketTimeout = 30000;// 请求超时时间
static int connectTimeout = 30000;// 传输超时时间
public static String doPost() {
String postUrl = "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx";
String soapAction = "";
String soapXml = getXML();
String retStr = "";
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpPost httpPost = new HttpPost(postUrl);
// 设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout)
.setConnectTimeout(connectTimeout).build();
httpPost.setConfig(requestConfig);
try {
httpPost.setHeader("Content-Type", "application/soap+xml;charset=UTF-8");
httpPost.setHeader("SOAPAction", soapAction);
StringEntity data = new StringEntity(soapXml, Charset.forName("UTF-8"));
httpPost.setEntity(data);
CloseableHttpResponse response = closeableHttpClient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
// 打印响应内容
retStr = EntityUtils.toString(httpEntity, "UTF-8");
}
// 释放资源
closeableHttpClient.close();
} catch (Exception e) {
}
return retStr;
}
cxf 和 httpclient 客户端调用 webservice 接口的更多相关文章
- So easy Webservice 3.使用HttpClient工具调用Webservice接口
首先,看看webservice服务调用演示: a) 登录http://www.webxml.com.cn b) 单击手机查询服务 c) 选择要调用的方法 例如: getMobileCodeInfo 输 ...
- WebService:CXF的JaxWsDynamicClientFactory实现调用WebService接口
首先需要引入依赖jar包 #版本只供参考,具体看项目 <dependency> <grouId>org.apache.cxf</grouId> <artifa ...
- Java之HttpClient调用WebService接口发送短信源码实战
摘要 Java之HttpClient调用WebService接口发送短信源码实战 一:接口文档 二:WSDL 三:HttpClient方法 HttpClient方法一 HttpClient方法二 Ht ...
- Java调用webservice接口方法
java调用webservice接口 webservice的 发布一般都是使用WSDL(web service descriptive langu ...
- php中创建和调用webservice接口示例
php中创建和调用webservice接口示例 这篇文章主要介绍了php中创建和调用webservice接口示例,包括webservice基本知识.webservice服务端例子.webservi ...
- java 调用webservice接口wsdl,推荐使用wsdl2java,放弃wsimport
网上说wsimport是jdk1.6后自带的客户端生成调用webservice接口的工具,其实我挺喜欢原生的东西,毕竟自家的东西用着应该最顺手啊,但往往让人惊艳的是那些集成工具. 本机jdk1.8.1 ...
- ThinkPHP使用soapclient调用webservice接口
1,开启 php.ini 这2个服务 12 extension=php_openssl.dllextension=php_soap.dll 以公共天气预报webservice为例,采用thinkPHP ...
- 使用soapui调用webservice接口
soapui是专门模拟调用webservice接口的工具,下面介绍下怎么使用: 1.下载soapui并安装: 2.以免费天气获取接口为例:http://www.webservicex.net/glob ...
- 使用JS调用WebService接口
<script> $(document).ready(function () { var username = "admin"; var password = &quo ...
随机推荐
- bzoj 2069 [ POI 2004 ] ZAW —— 多起点最短路 + 二进制划分
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2069 首先,对于和 1 相连的点,一定是从某个点出发,回到另一个点: 所以需要枚举起点和终点 ...
- 杂项:LDAP
ylbtech-杂项:LDAP 1.返回顶部 1. LDAP是轻量目录访问协议,英文全称是Lightweight Directory Access Protocol,一般都简称为LDAP.它是基于X. ...
- AcWing算法基础1.4
高精度 高精度加法,高精度减法,高精度乘低精度,高精度除以低精度,大概平时用的最多的就是这四个,模板有两种(因为我现在不太会用vector,就用数组也写了个,23333) 高精度运算和人工手算差不多, ...
- 如何写出网页高性能的DOM来提升网页的加载速度
为什么要写高性能DOM? 一个网站,在页面上承载最多内容的就是DOM,而且无论是我们通过加载JS.加载图片,他们也是通过写HTML标签来实现的.而我们性能优化要做的无非就是几大块: 站点的网络消耗 D ...
- RocketMQ(2)
1. 消费端集群消费(负载均衡) 示例代码: /** * Producer,发送消息 * */ public class Producer { public static void main(Stri ...
- android 中的Context(一)
context的功能如此强大,它是activity的父类. public abstract class Context { ... public abstract Object getSystemSe ...
- reactnative(1) - RefreshControl 使用案例
'use strict'; import React, { Component } from 'react'; import { AppRegistry, ScrollView, StyleSheet ...
- JavaScript入门笔记
第一章 JavaScript语法 1.1 初识JavaScript 1.3 数据类型 1.4 string和boolean类型 1.5 算数操作符 第二章 JavaScript流程控制语句 2.1 循 ...
- window.showModalDialog的问题
通过window.showModalDialog的方式弹出B页面,总报“拒绝访问”的错误,将站点添加到受信任站点可以解决这个问题
- drupal 8——图片组(list)在前台的显示顺序在登录状态和非登录状态不同
问题描述:该页面是通过view来输出的,然而,登录状态下其页面中的图片组输出顺序是乱序的,而非登录状态下则根据id值升序输出. 原因:在原view配置页面中,没有配置默认的排序字段 解决方案:在vie ...