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 = "" +
//                      " aaa 
";
       
       
        String soap =
                " " +
                "   " +
                "   " +
                "      " +
                "         " +
                "         11111 " +
                "     
" +
                "  
" +
                "
";
        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;

    }
}

 

 

 

 

 

你可能感兴趣的:(java.net 访问wsdl(soap))