Java调用WebService服务,简单好用,实例讲解

之前做项目遇到需要调用WebService的问题,这里就简单分享一下,

新手可以看看,老手就请飘过吧.

 

先把源代码拿出晒晒吧:

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;   
  
/***要导入dom4j-1.6.1.jar   
 * @author penghaijun  
 * @time 2011-10-17   
 * @title  dom4j调用webservice返回xml形式的String类型的数据(soap头请求方式)  
 * 根据url来定,浏览器中能得到什么,java程序这里就能输出什么  
 * 返回结果是一串XML,值也存在其中,需要自己去解析
 */  
@SuppressWarnings("unused")   
public class WebTest{   
    //远程WebService接口url,这里调用的是天气预报的服务   
    private static String NETDATA_URL = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";   
       
    //程序主方法入口,这个传入参数上海,查询上海的天气情况
    public static void main(String args[]){   
        Document document = getWeatherDocument("上海");   
        if(document != null){   
            System.out.println(document.asXML());   
        }   
    }   
       
    public static Document getWeatherDocument(String city){   
        Document document = null;   
        SAXReader reader = new SAXReader();   
        String str = "";   
        Map<String, String> map = new HashMap<String, String>();   
        reader.getDocumentFactory().setXPathNamespaceURIs(map);   
        try {   
            InputStream inputStream = getSoapInputStream(city);    //得到输入流   
            document = reader.read(inputStream);    //将输入流转化为document   
        } catch (Exception e){   
            e.printStackTrace();   
        }   
        return document;   
    }   
       
    public static InputStream getSoapInputStream(String city) throws Exception {   
        try{   
            String soap = getSoapRequest(city);   
            if(soap == null){   
                return null;   
            }   
            URL url = new URL(NETDATA_URL);   
            URLConnection conn = url.openConnection();   
            conn.setUseCaches(false);   
            conn.setDoInput(true);   
            conn.setDoOutput(true);   
            //参数参考http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?op=getWeatherbyCityName   
            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 outputStream = conn.getOutputStream();   
            OutputStreamWriter outputSW = new OutputStreamWriter(outputStream, "utf-8");   
            outputSW.write(soap);   
            outputSW.flush();   
            outputSW.close();   
            InputStream inputStream = conn.getInputStream();   
            return inputStream;   
        }catch(Exception e){   
            e.printStackTrace();   
            return null;   
        }   
    }   
       
    /**  
     * 获取soap请求头,并替换其中的标志符号为用户的自定义输入符号  
     * @param city 用户输入城市名  
     * @return 用户将要发送给服务器的soap请求  
     */  
    private static String getSoapRequest(String city){   
        StringBuilder sb = new StringBuilder();   
        //参数参考http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?op=getWeatherbyCityName   
        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();   
    }   
       
}

此类运行即可看到效果.

这个java类就可以帮咱们实现调用WebService服务,可以稍加修改配合Dwr使用,效果不错

用这种方式只需要一个Dom4j.jar的架包即可.

架包我已打包上传在115上,下载地址 : http://115.com/file/aq5qka6s#

如果发现下载地址共享时间过期,请联系我QQ: 254427888

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值