webservice使用的一些总结

转载自http://www.cnblogs.com/lm3515/archive/2010/10/29/1864456.html

 

WebService使用的一些总结

 

什么是WebService:

这个不用我在这里废话,网上的资料一搜一大把,若是你没有接触过这方面的知识,你能够先去网上查一下。这里我只想说一下我印象比较深入的几点:html

WebService是基于soap协议的。说实话这个知识刚开始我理解的并非很到位。这也在很长的时间局限了我在代码过程当中的思惟。我会在后面一个关于天气预报的实例中进行详细介绍的。总之,全部的webService请求、应答都是创建在soap协议的基础上的,而soap传输数据的载体是xml。关于soap协议的介绍,在W3C上有很详细的教程。java

WSDL是WebService的描述语言,它定义了Web Service作什么,怎么作和查询的信息。在验证一个WebService是否好用的时候,咱们一般会选则在浏览器中输入http://…….*?wsdl。若是显示出一个xml文件,咱们就认为这是好用的,反之就是不可用的。在项目刚刚开始的时候,我也是这么天真的认为的,由于教程上都是这么演示的。但事实并不是如此,就如同如下调用天气预报的wsdl。若是你在浏览器中输入http://fhs.6617.com/getweather.asmx?WSDL,它是不显示的,但事实证实这个webservice服务时可使用的(你能够经过http://fhs.6617.com/getweather.asmx?WSDL生成本地代理类)。这也一直是初学WebService者的一个误区。之因此不能经过浏览器访问,可能缘由是服务发布者,或者服务器作了一些限制,就如同互联网上不少服务器,咱们能够访问却ping不一样同样,服务者作了一些限制。(个人猜想)node

怎样使用WebService

是否是调用WebService时,必需要获得wsdl?必需要生成本地代理类?web

在回答这个问题,以前,我想先介绍一下网上一些资源:有不少热心的人,收集了不少经常使用的WebService,如:http://lqixv.javaeye.com/blog/308407。其中罗列的每一种服务,做者都提供了三种链接:Endpoint Disco WSDL 。其实这就是隐约的告诉咱们调用webservice服务的三种途径。apache

就如同咱们的项目,客户端在调用电信WebService上行发短信时,是经过 wsdl生成本地代理类方式实行的。而在接收状态报告下行时,采用的是 Endpoint方式(客户端不须要生成本地代理类,只须要知道Endpoint地址)。api

因此wsdl很重要,但并非必须的,前提你能经过api,规范文档等获取你须要的地址、命名空间的相关信息。浏览器

如何调用wsdl生成本地代理类:

经过wsdl生成本地代理类,有不少方式,既能够经过wsdl2java命令手动生成,也能够经过eclipse的axis2插件、xfire插件等。可是经过项目的实践,以为尽可能仍是经过axis2的wsdl2java的命令生成,会省去不少麻烦。好比说,若是你拥有不少的wsdl文件,他们又是相互联系的,这样你经过eclipse的插件生成时,会出现错误。由于它只能一下加载一个wsdl文件。这个问题在项目初期,让我吃了不少的苦头。。。。。服务器

Axis2是一个比较经常使用的WebService引擎,你们能够经过到http://ws.apache.org/axis2/下载,其中其中axis2-1.4.1-bin.zip文件中包含了Axis2中全部的jar文件,以及命令工具, axis2-1.4.1-war.zip文件则用于将WebService发布到Web容器中,网上有不少axis2教程,在这里再也不多说。app

天气预报调用实例:

下面的一个实例是调用天气预报的一个例子,没有采用wsdl生成本地代理类的方式,采用的是经过http请求直接访问服务端点的方法:dom

步骤是:

一、利用soap向webservice endpoint进行请求,取回请求结果

二、经过dom4J解析返回的xml流,获得所要的信息。

经过这个例子相信你们,会对”webserviice是基于soap协议”的这句话有更深入的理解,另外在使用dom4J解析xml返回流的过程当中,遇到了些麻烦,须要额外引入jaxen-1.1.1.jar包,不然程序会报org/jaxen/JaxenException的错误。而且在XML包含命名空间时,定位元素,须要按照xpath语法来写。

下面是类的源代码:

import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
 * 类做用调用webservice获得天气预报服务
 * @author qsw-Myonlystar 2010-1-13上午09:59:45
 */
public class Weather {
    /**
     * 获取soap请求头,并替换其中的标志符号为用户的输入符号
     * @param city 用户输入城市名
     * @return 用户将要发送给服务器的soap请求
     */
    private static String getSoapRequest(String city) {
        StringBuilder sb = new StringBuilder();
        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>    <getWeatherbyCityName xmlns=\"http://WebXml.com.cn/\">"
                        + "<theCityName>" + city
                        + "</theCityName>    </getWeatherbyCityName>"
                        + "</soap:Body></soap:Envelope>");
        return sb.toString();
    }
    /**
     * 用户把SOAP请求发送给服务器端,并返回服务器点返回的输入流
     * @param city 用户输入的城市名称
     * @return 服务器端返回的输入流,供客户端读取
     * @throws Exception
     */
    public static InputStream getSoapInputStream(String city) throws Exception {
        try {
            String soap = getSoapRequest(city);
            if (soap == null) {
                return null;
            }
            URL url = new URL(
                    "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx");
            URLConnection conn = url.openConnection();
            conn.setUseCaches(false);
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Length", Integer.toString(soap
                    .length()));
            conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
            conn.setRequestProperty("SOAPAction",
                    "http://WebXml.com.cn/getWeatherbyCityName");
            OutputStream os = conn.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
            osw.write(soap);
            osw.flush();
            osw.close();
            InputStream is = conn.getInputStream();
            //System.out.println(is.toString());
            return is;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 经过dom4j对服务器端返回的XML进行解析
     * @param city 用户输入的城市名称
     * @return 符串 用,分割
     */
    public static String getWeather(String city) {
        Document document=null;
        SAXReader reader = new SAXReader();
        String s="";
        Map map=new HashMap();
        map.put("design", "http://WebXml.com.cn/");
        reader.getDocumentFactory().setXPathNamespaceURIs(map);
        try {
            InputStream is = getSoapInputStream(city);//获得输入流
            document=reader.read(is);//将输入流转化为document
            String t=document.asXML();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        List nodes = document.selectNodes("//design:string");
        for (Iterator it = nodes.iterator(); it.hasNext();) {
            Element elm = (Element) it.next();
            String text=elm.getText();
            //System.out.println("fsffs"+text);
            s=s+elm.getText()+"\n";
        }
        return s;
    }
    /**
     * 测试函数
     * @param args
     */
    public static void main(String args[]){
        Weather w=new Weather();
        System.out.println(w.getWeather("泰安"));
    }
}

 

另外向你们推荐一个比较好的网站,http://www.webxml.com.cn/zh_cn/index.aspx。上面又不少好的webservice能够供咱们调用。

相关文章
相关标签/搜索