WebService开发方法介绍
1.使用客户端生成代码
- a.在命令行或终端使用wsimport生成客户端的调用
wsimport -s xxx http://127.0.0.1:12345/weathers?wsdl
xxx为本地存放生成代码的目录
- b.使用生成代码调用WebService服务端
//创建服务视图对象WeatherInterfaceImplService s=new WeatherInterfaceImplService();//通过服务试图得到PortTypeWeatherInterfaceImpl weatherInterfaceImplPort = s.getWeatherInterfaceImplPort();//调用方法weatherInterfaceImplPort.queryWeather("dasd");
2.使用JAXWS标准方法(Service)调用
- a.在命令行或终端使用wsimport生成客户端的调用
- b.在命令行或终端使用wsimport生成客户端的调用
//wsdl地址URL wsdldocument=new URL("http://127.0.0.1:54321/weathers?wsdl");//第一个参数是命名空间,第二个参数是Service名称QName qName=new QName("http://nio/","WeatherInterfaceImplService");//创建服务Service service =Service.create(wsdldocument, qName);//得到portTypeWeatherInterfaceImpl port = service.getPort(WeatherInterfaceImpl.class);port.queryWeather("广东");
3.模拟http客户端调用webservice服务
使用HttpURLConnection或apache的Httpclient模拟http请求,调用webservice。
注意:使用此方法不需要生成客户端调用代码。
下面是一个例子供大家参考、
package nio;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.ArrayList;
import java.util.List;import javax.net.ssl.SSLContext;import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;public class HttpClientTest { @Test public void jUnitTest() { get(); } /** * HttpClient连接SSL */ public void ssl() { CloseableHttpClient httpclient = null; try { KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); FileInputStream instream = new FileInputStream(new File("d:\\tomcat.keystore")); try { // 加载keyStore d:\\tomcat.keystore trustStore.load(instream, "123456".toCharArray()); } catch (CertificateException e) { e.printStackTrace(); } finally { try { instream.close(); } catch (Exception ignore) { } } // 相信自己的CA和所有自签名的证书 SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()).build(); // 只允许使用TLSv1协议 SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); // 创建http请求(get方式) HttpGet httpget = new HttpGet("https://localhost:8443/myDemo/Ajax/serivceJ.action"); System.out.println("executing request" + httpget.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httpget); try { HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); System.out.println(EntityUtils.toString(entity)); EntityUtils.consume(entity); } } finally { response.close(); } } catch (IOException e) { e.printStackTrace(); } catch (KeyManagementException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyStoreException e) { e.printStackTrace(); } finally { if (httpclient != null) { try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * post方式提交表单(模拟用户登录请求) */ public void postForm() { // 创建默认的httpClient实例. CloseableHttpClient httpclient = HttpClients.createDefault(); // 创建httppost HttpPost httppost = new HttpPost("http://localhost:8080/myDemo/Ajax/serivceJ.action"); // 创建参数队列 List<NameValuePair> formparams = new ArrayList<NameValuePair>(); formparams.add(new BasicNameValuePair("username", "admin")); formparams.add(new BasicNameValuePair("password", "123456")); UrlEncodedFormEntity uefEntity; try { uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8"); httppost.setEntity(uefEntity); System.out.println("executing request " + httppost.getURI()); CloseableHttpResponse response = httpclient.execute(httppost); try { HttpEntity entity = response.getEntity(); if (entity != null) { System.out.println("--------------------------------------"); System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8")); System.out.println("--------------------------------------"); } } finally { response.close(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 关闭连接,释放资源 try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 发送 post请求访问本地应用并根据传递参数不同返回不同结果 */ public void post() { // 创建默认的httpClient实例. CloseableHttpClient httpclient = HttpClients.createDefault(); // 创建httppost HttpPost httppost = new HttpPost("http://localhost:8080/myDemo/Ajax/serivceJ.action"); // 创建参数队列 List<NameValuePair> formparams = new ArrayList<NameValuePair>(); formparams.add(new BasicNameValuePair("type", "house")); UrlEncodedFormEntity uefEntity; try { uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8"); httppost.setEntity(uefEntity); System.out.println("executing request " + httppost.getURI()); CloseableHttpResponse response = httpclient.execute(httppost); try { HttpEntity entity = response.getEntity(); if (entity != null) { System.out.println("--------------------------------------"); System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8")); System.out.println("--------------------------------------"); } } finally { response.close(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 关闭连接,释放资源 try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 发送 get请求 */ public void get() { CloseableHttpClient httpclient = HttpClients.createDefault(); try { // 创建httpget. HttpGet httpget = new HttpGet("http://www.baidu.com/"); System.out.println("executing request " + httpget.getURI()); // 执行get请求. CloseableHttpResponse response = httpclient.execute(httpget); try { // 获取响应实体 HttpEntity entity = response.getEntity(); System.out.println("--------------------------------------"); // 打印响应状态 System.out.println(response.getStatusLine()); if (entity != null) { // 打印响应内容长度 System.out.println("Response content length: " + entity.getContentLength()); // 打印响应内容 System.out.println("Response content: " + EntityUtils.toString(entity)); } System.out.println("------------------------------------"); } finally { response.close(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 关闭连接,释放资源 try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 上传文件 */ public void upload() { CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpPost httppost = new HttpPost("http://localhost:8080/myDemo/Ajax/serivceFile.action"); FileBody bin = new FileBody(new File("F:\\image\\sendpix0.jpg")); StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN); HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("bin", bin).addPart("comment", comment).build(); httppost.setEntity(reqEntity); System.out.println("executing request " + httppost.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httppost); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); HttpEntity resEntity = response.getEntity(); if (resEntity != null) { System.out.println("Response content length: " + resEntity.getContentLength()); } EntityUtils.consume(resEntity); } finally { response.close(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } }
}
WebService开发方法介绍相关推荐
- 敏捷开发:5种主流开发方法介绍
一.极限编程 极限编程(ExtremeProgramming,简称XP)是由KentBeck在1996年提出的.极限编程是一个轻量级的.灵巧的软件开发方法:同时它也是一个非常严谨和周密的方法.XP是一 ...
- 【系分论文】论信息系统开发方法及应用
目录 论题 论题介绍 论文要点 理论素材准备 范文 摘要 正文 论题 论信息系统开发方法及应用 论题介绍 信息系统师一个复杂的人机交互系统,它不仅包含计算机技术,软件技术,通信技术,网络技术以及其他工 ...
- SAP UI5 应用开发教程之六十二 - 基于 OData V4 的 SAP UI5 表格控件使用方法介绍试读版
一套适合 SAP UI5 初学者循序渐进的学习教程 教程目录 SAP UI5 本地开发环境的搭建 SAP UI5 应用开发教程之一:Hello World SAP UI5 应用开发教程之二:SAP U ...
- html如何添加web字体,html中字体如何实现加粗(方法介绍)_WEB前端开发,html,字体加粗...
PS五角星形状使用方法?_WEB前端开发 PS五角星形状使用方法:首先点击左侧工具栏中的"矩形工具"子选项中的"多边形工具":然后点击上方工具栏中的" ...
- 【Unity 3D游戏开发】在Unity使用NoSQL数据库方法介绍
随着游戏体积和功能的不断叠加,游戏中的数据也变得越来越庞杂,这其中既包括玩家产生的游戏存档等数据,例如关卡数.金币等,也包括游戏配置数据,例如每一关的配置情况.尽管Unity提供了PlayerPref ...
- 软件过程开发方法(RUP、AP、MP、HP) CMMI/SPCA业务介绍
软件开发一个复杂的活动, 它包含了需求调研, 系统设计, 开发, 部署, 维护等活动. 而且现有规范和流程目的并不是让你去完成文档, 而是通过这些文档, 让软件的质量更能得到保证.组成软件开发和系 ...
- 项目管理基础:软件开发的方法介绍
软件开发方法主要有结构化方法.原型化方法.面向对象开发方法.敏捷方法. 1.结构化方法 结构化方法由结构化分析.结构化设计.结构化程序设计组成,它是一种面向数据流的开发方法. 结构化分析:依据分解与抽 ...
- python 全栈开发,Day36(作业讲解(大文件下载以及进度条展示),socket的更多方法介绍,验证客户端链接的合法性hmac,socketserver)...
先来回顾一下昨天的内容 黏包现象 粘包现象的成因 : tcp协议的特点 面向流的 为了保证可靠传输 所以有很多优化的机制 无边界 所有在连接建立的基础上传递的数据之间没有界限 收发消息很有可能不完全 ...
- 常规平台刷机方法介绍-ROM开发入门到精通
常规平台刷机方法介绍 1.MTK刷机教程 一.安装驱动(成功安装驱动是刷机的前提) 驱动安装.解压,然后选择驱动自动安装: 二.刷机 解压刷机工具,如图: 2,双击打开工 3,选择刷机包,找到包的文件 ...
最新文章
- 【编程练习】C语言debug合集
- opencv python 官方文档里的“sa”关键字是什么意思?(see also)
- Mycat社区出版: 分布式数据库架构及企业实践——基于Mycat中间件
- 计算机组成加减交替法被除数,计算机组成原第2章答案.doc
- php mysql 地理位置_PHP MySql和地理位置
- Oracle DBA课程系列笔记(4)
- CocoaPods 添加第三方库报错
- 【PL/SQL】 使用游标
- cvCloneImage()内存泄漏解决方法, cvCloneImage()和cvCopy()的区别
- 51 nod 最长公共子序列问题(打印路径)
- 《数据库系统》期末复习知识点总结(全)
- 移远NBIOT BC95-B5使用CoAP协议接入华为IoT平台第一篇
- mysql executereader_ExecuteReader的用法
- 最强蜗牛服务器维护祷告什么时候领取都一样,最强蜗牛猴子祷告奖励什么时候领...
- 第三章 输入验证----tapestry教程Enjoying Web DevelopmenEnjoying Development翻译
- HTML5表白小程序
- 使用CVXQUAD时出现,函数或变量 ‘op_rel_entr_epi_cone‘ 无法识别。
- Cannot mix different versions of joi schemas
- 打篮球与企业管理有相似之处吗?
- 私域论坛圈子社区小程序开发