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;

    }
}

猜你喜欢

转载自chun521521.iteye.com/blog/2372739
今日推荐