java遠程調用soap協議接口


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.*;
 
public class SoapUtil {
    
    /*
     * 遠程訪問SOAP協議接口
     * 
     * @param url: 服務接口地址"http://192.168.0.120:8222/HelloWorld?wsdl"
     * @param isClass:接口類名
     * @param isMethod: 接口方法名
     * @param sendSoapString: soap協議xml格式訪問接口
     *    
     * @return  soap協議xml格式
     * 
     * @備注:有四種請求頭格式1、SOAP 1.1; 2、SOAP 1.2 ; 3、HTTP GET; 4、HTTP POST
     * 參考---》http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?op=getWeatherbyCityName
     */
    public static String getWebServiceAndSoap(String url,String isClass,String isMethod,StringBuffer sendSoapString) throws IOException {
        String soap = sendSoapString.toString();
        if (soap == null) {
            return null;
        }
        URL soapUrl = new URL(url);
        URLConnection conn = soapUrl.openConnection();
        conn.setUseCaches(false);
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestProperty("Content-Length",
                Integer.toString(soap.length()));
        conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
        // 調用的接口方法是
        conn.setRequestProperty(isClass,isMethod);
        OutputStream os = conn.getOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
        osw.write(soap);
        osw.flush();
        osw.close();
        // 獲取webserivce返回的流
        InputStream is = conn.getInputStream();
        if (is!=null) {
            byte[] bytes = new byte[0];
            bytes = new byte[is.available()];
            is.read(bytes);
            String str = new String(bytes);
            return str;
        }else {
            return null;
        }
    }
}
import java.io.IOException;
 
public class Test {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        StringBuffer sendSoapString = new StringBuffer();
        sendSoapString.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tes=\"http://test03/\">");
        sendSoapString.append("   <soapenv:Header/>");
        sendSoapString.append("   <soapenv:Body>");
        sendSoapString.append("      <tes:getSum>");
        sendSoapString.append("         <arg0>66</arg0>");
        sendSoapString.append("         <arg1>33</arg1>");
        sendSoapString.append("      </tes:getSum>");
        sendSoapString.append("   </soapenv:Body>");
        sendSoapString.append("</soapenv:Envelope>");
        
        try {
            String ret= SoapUtil.getWebServiceAndSoap("http://192.168.0.120:8222/HelloWorld?wsdl","HelloWorld","getSum", sendSoapString);
            System.out.println(ret);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
 
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM