惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
博客园 - 聂微东
Application and Cybersecurity Blog
Application and Cybersecurity Blog
MyScale Blog
MyScale Blog
罗磊的独立博客
H
Help Net Security
L
LangChain Blog
T
Threat Research - Cisco Blogs
量子位
S
Securelist
Last Week in AI
Last Week in AI
L
Lohrmann on Cybersecurity
T
The Exploit Database - CXSecurity.com
P
Privacy International News Feed
The Hacker News
The Hacker News
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Blog of Author Tim Ferriss
T
Threatpost
Security Latest
Security Latest
P
Palo Alto Networks Blog
Microsoft Security Blog
Microsoft Security Blog
NISL@THU
NISL@THU
F
Full Disclosure
WordPress大学
WordPress大学
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
C
Check Point Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Heimdal Security Blog
J
Java Code Geeks
Recorded Future
Recorded Future
Hugging Face - Blog
Hugging Face - Blog
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
阮一峰的网络日志
阮一峰的网络日志
U
Unit 42
B
Blog RSS Feed
月光博客
月光博客
C
Cisco Blogs
V
Visual Studio Blog
D
DataBreaches.Net
H
Hacker News: Front Page
博客园 - 叶小钗
N
News and Events Feed by Topic
爱范儿
爱范儿
A
Arctic Wolf

博客园 - 轻松

css div 垂直居中 How to create custom methods for use in spring security expression language annotations How to check “hasRole” in Java Code with Spring Security? Android 显示/隐藏 应用图标 Android 当媒体变更后,通知其他应用重新扫描 Dynamically loading an external JavaScript or CSS file android 脸部抠图 IIS + TOMCAT 注意事项 textbox icon jquery 插件 让IE支持HTML5的Canvas vba 生成euc文件的方法 - 轻松 - 博客园 数值项目的格式化 - 轻松 - 博客园 [原创]让Allvidoes插件支持 优酷(www.youku.com)的视频 ORACLE ERROR CODE代表的意思 http://hi.baidu.com/czh0221/blog/item/9f0447e719644a2ab83820c6.html [转]利用location的hash值来解决Ajax两大难题 xampp-control中启动Apache时80断口被占用的错误 asp.net mail java获取运行的jar(class)文件的路径
JAVA 调用Web Service的方法
轻松 · 2009-02-03 · via 博客园 - 轻松

1.使用HttpClient
用到的jar文件:commons-httpclient-3.1.jar
方法:
预先定义好Soap请求数据,可以借助于XMLSpy Professional软件来做这一步生成。

String soapRequestData = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
    
"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">" +
      "<soap12:Body>" +
       
" <getCountryCityByIp xmlns=\"http://WebXml.com.cn/\">" +
      "    <theIpAddress>219.137.167.157</theIpAddress>" +
     
"   </getCountryCityByIp>" +
    
"  </soap12:Body>" +
    
"</soap12:Envelope>";

然后定义一个PostMethod,这时需要指定web服务的Url;

PostMethod postMethod = new PostMethod(“http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx”);

然后把Soap请求数据添加到PostMethod中

byte[] b = soapRequestData.getBytes("utf-8");
InputStream is 
= new ByteArrayInputStream(b,0,b.length);
RequestEntity re 
= new InputStreamRequestEntity(is,b.length,"application/soap+xml; charset=utf-8");
postMethod.setRequestEntity(re);

最后生成一个HttpClient对象,并发出postMethod请求

HttpClient httpClient = new HttpClient();
statusCode 
= httpClient.executeMethod(postMethod);
String soapRequestData 
=  postMethod.getResponseBodyAsString();

soapRequestData就是调用web服务的Soap响应数据,是xml格式的,可以通过解析soapRequestData来获得调用web服务的返回值。

2.使用Xfire
用到的jar文件xfire-all-1.2.4.jar, jdom-1.0.jar
方法:
定义一个Client对象,指定web服务的wsdl的地址

Client c = new Client(new URL(“http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl”));

调用Client对象的invoke方法,指定web服务的方法名,和参数,返回值是一个Object型的数组。
下面代码调用getVersionTime方法,这个方法没有参数用所以后一个参数使用new Object[0]。

Object[] results = c.invoke(“getVersionTime”, new Object[0]);

3.使用axis2
下载axis2-1.4
方法:
打开控制台,进入axis2-1.4/bin目录

wsdl2java.bat -uri http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl -p ws.clinet.axis2


上述命令执行完后,会在当前目录下生成一个src目录,在src\ ws\ clinet\ axis2目录里生成XXXXCallbackHandler.java和XXXXStub.java两个文件。
wsdl2java 会根据wsdl文件生成web服务的调用接口,参数类,返回值的类。
在调用webservice的时候直接实例化一个XXXXStub的对象,然后调用web服务的方法就可以了。

4. 总结
针对某种工具搭建的Web Service服务可能有与其对应的更简单的调用方法,在这里没有做描述,上述的调用web服务的方法是通用的。
上述三种方法中使用httpclient应该是比较灵活,但是开发效率低,难度大,使用Xfire和axis2比较容易,开发速度快,但是axis2通用性不好,有的web服务用axis2不好用。httpclient和Xfire通用性比较好,鉴于以上特点推荐使用Xfire。