使用HttpURLConnection调用soap webservice(来源网络)

import java.io.BufferedReader;  
import java.io.InputStreamReader;  
import java.io.OutputStream;  
import java.net.HttpURLConnection;  
import java.net.URL;
import java.util.List;
import java.util.Scanner;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.junit.Test;  

public class HttpUtil {  
  
    public static String sendSoapPost(String url, String xml,  
            String contentType, String soapAction) {  
        String urlString = url;  
        HttpURLConnection httpConn = null;  
        OutputStream out = null;  
        String returnXml = "";  
        try {  
            httpConn = (HttpURLConnection) new URL(urlString).openConnection();  
            httpConn.setRequestProperty("Content-Type", contentType);  
            if (null != soapAction) {  
                httpConn.setRequestProperty("SOAPAction", soapAction);  
            }  
            httpConn.setRequestMethod("POST");  
            httpConn.setDoOutput(true);  
            httpConn.setDoInput(true);  
            httpConn.connect();  
            out = httpConn.getOutputStream(); // 获取输出流对象  
            httpConn.getOutputStream().write(xml.getBytes("UTF-8")); // 将要提交服务器的SOAP请求字符流写入输出流  
            out.flush();  
            out.close();  
            int code = httpConn.getResponseCode(); // 用来获取服务器响应状态  
            String tempString = null;  
            StringBuffer sb1 = new StringBuffer();  
            if (code == HttpURLConnection.HTTP_OK) {  
                BufferedReader reader = new BufferedReader(  
                        new InputStreamReader(httpConn.getInputStream(),  
                                "UTF-8"));  
                while ((tempString = reader.readLine()) != null) {  
                    sb1.append(tempString);  
                }  
                if (null != reader) {  
                    reader.close();  
                }  
            } else {  
                BufferedReader reader = new BufferedReader(  
                        new InputStreamReader(httpConn.getErrorStream(),  
                                "UTF-8"));  
                // 一次读入一行,直到读入null为文件结束  
                while ((tempString = reader.readLine()) != null) {  
                    sb1.append(tempString);  
                }  
                if (null != reader) {  
                    reader.close();  
                }  
            }  
            // 响应报文  
            returnXml = sb1.toString();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return returnXml;  
    }  
    @Test
    public void Test() throws DocumentException {  
    	
        String url = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx";  
  
        StringBuilder sb = new StringBuilder("");  
        System.out.println("输入手机号:");
        @SuppressWarnings("resource")
		Scanner scan=new Scanner(System.in);
        String phone=scan.next();
          sb.append("<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/\">")
            .append("  <soap:Body>" )
        	.append("    <getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">" ) 
        	.append("      <mobileCode>"
        			+ phone
        			+ "</mobileCode>" ) 
        	.append("      <userID></userID>" )
        	.append("    </getMobileCodeInfo>") 
        	.append("  </soap:Body>" )
        	.append("</soap:Envelope>");
        

        String dataXml = sb.toString();  
        //System.out.println(dataXml);
        String contentType = "text/xml; charset=utf-8";  
        String soapAction = "http://WebXml.com.cn/getSupportProvince"; 
        		soapAction="http://WebXml.com.cn/getMobileCodeInfo";
        // String soapAction =  
        // "\"document/http://pengjunnlee.com/CustomUI:GetWeatherById\"";  
        String resultXml = HttpUtil.sendSoapPost(url, dataXml, contentType,  
                soapAction); 
        
        //使用dom4j解析String Xml
        Document document = DocumentHelper.parseText(resultXml);
        //Element root = document.getRootElement();
        @SuppressWarnings("unchecked")
		List<Element> list = document.selectNodes("/soap:Envelope/*[name()='soap:Body']/*[name()='getMobileCodeInfoResponse']/*[name()='getMobileCodeInfoResult']");
        for (Element element : list) {
        	String str=element.getText();
        	System.out.println(str); 
		}
        
        
    }  
    
    @Test
    public void test2() throws DocumentException {
    	
    	for (int i = 0; i > -1; i++) {
    		Test();
		}
    	
    	
    }
}  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值