java远程调用soap协议接口

Posted on   jiaoqing。  阅读(1233)  评论(0编辑  收藏  举报
复制代码
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();
        }
    }
 
}
复制代码

 

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
历史上的今天:
2021-04-07 【linux】【tomcat】linux下定时重启tomcat 【CentOS 6.4】【CentOS 7.6】

随笔 - 287, 文章 - 0, 评论 - 3, 阅读 - 42万

Copyright © 2025 jiaoqing。
Powered by .NET 9.0 on Kubernetes

点击右上角即可分享
微信分享提示