前几天项目项目中红需要调用webservice接口(.asmx),简单的记录一下

本文介绍了一种使用Java进行WebService调用的方法,通过SOAP协议发送请求,并利用DOM4J库解析返回的XML数据。示例代码展示了如何构造SOAP请求,设置HTTP连接属性,以及如何从服务端接收并解析响应。


public class WebUtil {

	
	public static String service(String xml,String op,String surl) throws IOException {
		  
	    // 需要调用的方法
		String soapActionString = "http://tempuri.org/"+op;
	  
        //第一步:创建服务地址,不是WSDL地址 
        //URL url = new URL("http://IP地址/WebService.asmx");  
		URL url = new URL(surl);  
        //第二步:打开一个通向服务地址的连接  
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
        
        // 拼接soap
		String soap = xml;
		byte[] buf = soap.getBytes("UTF-8");
        
        //第三步:设置参数    
        //3.1发送方式设置:POST必须大写  
        connection.setRequestMethod("POST");
        //3.2设置数据格式:content-type  
        connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
        //3.3设置SOPEAction
        connection.setRequestProperty("soapActionString", soapActionString);
        //3.4设置输入输出,因为默认新创建的connection没有读写权限,  
        connection.setDoInput(true);  
        connection.setDoOutput(true);

        OutputStream out = connection.getOutputStream();
		out.write(buf);
		out.close();
        //第4步:接收服务端响应,打印(xml格式数据)
        int responseCode = connection.getResponseCode();  
        StringBuffer sb = new StringBuffer();
    	if (200 == responseCode) {
			InputStream is = connection.getInputStream();
			byte[] b = new byte[1024];
			int len = 0;
			while ((len = is.read(b)) != -1) {
				String s = new String(b, 0, len, "utf-8");
				sb.append(s);
			}
			is.close();
		}
        return sb.toString();
    }

    /** 
     *  <?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> 
            <getMobileCodeInfo xmlns="http://WebXml.com.cn/"> 
              <mobileCode>string</mobileCode> 
              <userID>string</userID> 
            </getMobileCodeInfo> 
          </soap:Body> 
        </soap:Envelope> 
     * @param phoneNum 
     * @return 
     */  
  
  public static void main(String[] args) throws IOException {

        //传入的参数  具体的 看你的接口
	 	String soapXML =  "<?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>"
    			    +"<GetDevicaInfoByParameter xmlns='http://tempuri.org/'>"
    			      +"<usercode>用户名</usercode>"
    			      +"<userpassword>密码</userpassword>"
    			      +"<param>"
    			      	+"<FADD>2640FC1418438016</FADD>"
    			      	+"<FADD_Name>xxxxx</FADD_Name>"
    			     +"</param>"
    			      +"<pageIndex>1</pageIndex>"
    			      +"<rowsCount>1</rowsCount>"
    			   +"</GetDevicaInfoByParameter>"
    			  +"</soap:Body>"
    			+"</soap:Envelope>";
	 	
	 	String service= service(soapXML,"GetDevicaInfoByParameter","http://ip地址/WebService.asmx");
	 	
	 	
	 	jxXml(service);
	 	
	 	System.out.println(cl6);
 }

	private static void  jxXml(String service) {
		
		try {
			// 将字符串转为XML
			Document doc = DocumentHelper.parseText(service);
			
			//获取文档的根节点
			 Element rootElt = doc.getRootElement(); // 获取根节点
			//System.out.println("根节点:"+rootElt); //根节点名称   Envelope
			//获取子body的子节点的子节点
			Element element = rootElt.element("Body").element("GetDevicaInfoByParameterResponse").element("GetDevicaInfoByParameterResult");
			
			//获取所有DeviceDataModel下的值
		    List<Element> elements = element.element("Data").elements("DeviceDataModel");
		    
		    //循环 elements
		    for (Element element2 : elements) {

			}
		    
			
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	} 
	
	
}

用的是dom4j解析 xml

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值