废话不多说,上次说道发布自己的webservice,这次我们来看看如何调用别人写的webservice,这是一个牛人写的代码,我对他进行了简单的分析,大家可以参考参考:
/**
* @author :LYL
*@date:2011-4-20,下午05:04:10
*/
package com.lyl.webservice;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class WeatherClient {
//服务主机
private static String SERVICES_HOST = "www.webxml.com.cn";
//天气服务URL
private static String WEATHER_SERVICES_URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/";
//获取省份code的URL
private static String PROVINCE_CODE_URL = WEATHER_SERVICES_URL
+ "getRegionProvince";
//获取城市code的URL
private static String CITY_CODE_URL = WEATHER_SERVICES_URL
+ "getSupportCityString?theRegionCode=";
//天气查询的URL
private static String WEATHER_QUERY_URL = WEATHER_SERVICES_URL
+ "getWeather?theUserID=&theCityCode=";
private WeatherClient(){}
public static void main(String[] args) throws Exception{
int provinceCode = getProvinceCode("江苏");
int cityCode = getCityCode(provinceCode, "苏州");
List<String> weatherList = getWeather(cityCode);
for(String weather:weatherList){
System.out.println(weather);
}
}
/**
* 得到省份ID
* @param provinceName
* @return
*/
public static int getProvinceCode(String provinceName){
Document document;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//设定支持命名空间
dbf.setNamespaceAware(true);
int provinceCode = 0;
try{
//通过文档构建器工厂获取一个文档构建器
DocumentBuilder db = dbf.newDocumentBuilder();
//获得province_code的输入流
InputStream inputStream = getSoapInputStream(PROVINCE_CODE_URL); //具体webService相关
document = db.parse(inputStream);
//获取所有标签名为string的节点
NodeList nodeList = document.getElementsByTagName("string"); //具体webService相关
int len = nodeList.getLength();
for(int i = 0; i < len; i++){
Node n = nodeList.item(i);
//获得省份名和对应的代码
String result = n.getFirstChild().getNodeValue();
//将省份名和代码区分开
String[] address = result.split(",");
String pName = address[0];
String pCode = address[1];
//判断是否是我们需要的省份
if(pName.equalsIgnoreCase(provinceName)){
provinceCode = Integer.parseInt(pCode);
}
}
//关闭流
inputStream.close();
}catch(DOMException e){
e.printStackTrace();
}catch(ParserConfigurationException e){
e.printStackTrace();
}catch (SAXException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return provinceCode;
}
/**
* 获取城市ID
* @param provinceCode
* @param cityName
* @return
*/
public static int getCityCode(int provinceCode, String cityName){
Document document;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
int cityCode = 0;
try{
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream inputStream = getSoapInputStream(CITY_CODE_URL + provinceCode); //具体webService相关
document = db.parse(inputStream);
NodeList nl = document.getElementsByTagName("string"); //具体webService相关
int len = nl.getLength();
for(int i = 0; i < len; i++){
Node n = nl.item(i);
String result = n.getFirstChild().getNodeValue();
String[] address = result.split(",");
String cName = address[0];
String cCode = address[1];
if(cName.equalsIgnoreCase(cityName)){
cityCode = Integer.parseInt(cCode);
}
}
inputStream.close();
}catch(DOMException e){
e.printStackTrace();
}catch(ParserConfigurationException e){
e.printStackTrace();
}catch (SAXException e){
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
return cityCode;
}
/**
* 取得url连接返回的输入流
* @param url
* @return
*/
public static InputStream getSoapInputStream(String url){
InputStream inputStream = null;
try{
URL urlObj = new URL(url);
//得到URL所引用的远程对象的连接
URLConnection urlConn = urlObj.openConnection();
//设置一般请求属性
urlConn.setRequestProperty("Host", SERVICES_HOST); //具体webService相关
urlConn.connect();
//返回从此打开的连接读取的输入流
inputStream = urlConn.getInputStream();
}catch(MalformedURLException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return inputStream;
}
/**
* 获取天气信息
* @param cityCode
* @return
*/
public static List<String> getWeather(int cityCode){
List<String> weatherList = new ArrayList<String>();
Document document;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
try{
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream inputStream = getSoapInputStream(WEATHER_QUERY_URL + cityCode);
document = db.parse(inputStream);
NodeList nl = document.getElementsByTagName("string");
int len = nl.getLength();
for(int i = 0; i < len; i++){
Node n = nl.item(i);
String weather = n.getFirstChild().getNodeValue();
weatherList.add(weather);
}
inputStream.close();
}catch(UnsupportedEncodingException e){
e.printStackTrace();
}catch (DOMException e){
e.printStackTrace();
}catch (ParserConfigurationException e){
e.printStackTrace();
}catch(SAXException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
return weatherList;
}
}
大家如果想自己编写可以参考此代码,至于调用何种服务要跟具体的服务提供商有关,大家可以用此例子试验下。
分享到:
相关推荐
本案例实现使用Axis2生成.arr包发布WebService服务,并使用Axis2生成客户端代码方式调用WebService
java 调用https发布的webservice接口实例及axis包及详细说明
使用Java+Axis2调用Web Services 网络接口,代码中列举4个例子可以查看,有详细注释提供参考。
Java Axis 直接调用webservice 接口工具类, 无需根据wsdl编译客户端,需要axis相关包,测试使用axis 1.4, 附demo引入axis包后直接可以测试;
java调用webservice的jar包
webseservice 客户端调用,java发布的axis2 webservice,通过 js调用调用webservice,普通ajax实现,里面有详细注释,代码简单明了,我自己做过测试。希望对大家有所帮助。
Java WebService 简单实例 方式二(axis1直接调用方式)
本方法是用axis1.4技术,实现java客户端调用webservice。已经可实现过可行的,如果不行可加我QQ号302633进行详细解析。
一个java编写基于Axis1.4的webservice接口,包括服务端接口和调用端接口。
java调用webservice,axis方式,从后台到页面一整套的demo供大家参考。最简单的demo,所以用的servlet写的。 鼓捣了两天,希望对大家有帮助,也为自己以后使用留个备份。有不明白的可以给我留言
JAVA调用webservice(axis方式).doc
Java通过Axis2调用SAP的Webservice 生成Java Proxy
1)Java用axis发布/调用webservice demo;调用方式为wsdl2java生成本地client代码调用; 2)资源中包含代码和axis1.4安装文件;
java axis客户端调用webservice,可应用于ofbiz框架。 解决网络上共享代码两次调用后会出现timeout的BUG
Axis2教程和java调用webservice的各种方法总结
NULL 博文链接:https://chen106106.iteye.com/blog/1069463
JAVA WEBSERVICE 建的实例工程,给PB调用, 因VS建的 WEBSERVICE PB很难调用,所以用JAVA的axis做了个小实例 共大家参考
java axis 调用C# webService所需的jar包
本人是做java开发,所以想研究一下怎么用java开发webservice供delphi7调用 javaserver 是WebService 服务端,采用AXIS 1.4 为什么采用AXIS1.4? 因为经过我测试,Delphi7版本太老了,不兼容JAX-WS和AXIS2 另外我...
java客户端程序调用webservice接口