java.net 访问wsdl(soap)

 

package com.jit.common;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
public class testSoap {
    public static  void main(String[] args) throws Exception {
        //服务的地址
        URL wsUrl = new URL("http://172.16.15.109:1010/uiasmag/wsauth/userWSService");
       
        HttpURLConnection conn = (HttpURLConnection) wsUrl.openConnection();
       
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
       
        OutputStream os = conn.getOutputStream();
       
        //请求体
//        String soap = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:q0=\"http://ws.itcast.cn/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
//                      "<soapenv:Body> <q0:sayHello><arg0>aaa</arg0>  </q0:sayHello> </soapenv:Body> </soapenv:Envelope>";
       
       
        String soap =
                "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:wss=\"http://wsservice.userInfo.uias.jit.com/\"> " +
                "   <soapenv:Header/> " +
                "   <soapenv:Body> " +
                "      <wss:lockUser> " +
                "         <!--Optional:--> " +
                "         <userId>11111</userId> " +
                "      </wss:lockUser> " +
                "   </soapenv:Body> " +
                "</soapenv:Envelope>";
        os.write(soap.getBytes());
       
        InputStream is = conn.getInputStream();
       
        byte[] b = new byte[1024];
        int len = 0;
        String s = "";
        while((len = is.read(b)) != -1){
            String ss = new String(b,0,len,"UTF-8");
            s += ss;
        }
        System.out.println(s);
        String aaa =  print(s);
        System.out.println(aaa);
        is.close();
        os.close();
        conn.disconnect();
    }
   
    public static String print(String xmlStr){
        String rtnString = "";
        Document doc;
        try {
            doc = DocumentHelper.parseText(xmlStr);
              Element rootElt = doc.getRootElement(); // 获取根节点
              System.out.println("根节点:" + rootElt.getName()); // 拿到根节点的名称
              Iterator body = rootElt.elementIterator("Body");
              while (body.hasNext()) {
                   Element recordEless = (Element) body.next();
                   Iterator cdGasFyResponse = recordEless.elementIterator("lockUserResponse");
                   while(cdGasFyResponse.hasNext()){
                       Element returnValue = (Element)cdGasFyResponse.next();
                       Iterator returnIt = returnValue.elementIterator("return");
                       while(returnIt.hasNext()){
                           Element element1  = (Element)returnIt.next();
                           rtnString = element1.getTextTrim();
                           System.out.println(element1.getTextTrim());
                       }
                   }
               }
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return rtnString;

    }
}

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值