soap客户端代码示例

复制代码
public static String connectToBytterForpay(Context context, String xml, String WSDLAddress) throws Exception{
        HttpURLConnection connection = null;
        StringBuffer sb = new StringBuffer();
        OutputStream os = null;
        InputStream is = null;
        try{
            URL u = new URL(WSDLAddress);
            connection = (HttpURLConnection)u.openConnection();
            connection.setConnectTimeout(15000);
            connection.setReadTimeout(60000);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
            connection.setRequestProperty("SOAPAction", "bossWebService");
            connection.setDoOutput(true);
            os = connection.getOutputStream();
            String soapBody = "<![CDATA[" + xml + "]]>";//具体报文,用<![CDATA[ ]]>包起来
            sb.append(
                    "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://server.webservice.standard.erp.hibernate.byttersoft.com\"><soapenv:Header/><soapenv:Body><ser:PaymentSysXmlData><ser:payXml>");
            sb.append(soapBody);
            sb.append("</ser:payXml></ser:PaymentSysXmlData></soapenv:Body></soapenv:Envelope>");
            os.write(sb.toString().getBytes("utf-8"));
            os.flush();
            sb = new StringBuffer();
            is = connection.getInputStream();
            String line = "";
            for(BufferedReader in = new BufferedReader(new InputStreamReader(is, "utf-8")); in.ready(); sb
                    .append(line))
            {
                line += in.readLine();
            }
            MessageFactory msgFactory;
            msgFactory = MessageFactory.newInstance();
            SOAPMessage reqMsg =
                    msgFactory.createMessage(new MimeHeaders(), new ByteArrayInputStream(line.getBytes("utf-8")));
            reqMsg.saveChanges();
            SOAPBody body = reqMsg.getSOAPBody();
            Iterator<SOAPElement> iterator = body.getChildElements();
            String rcvxml = "";
            while(iterator.hasNext()){
                SOAPElement element = (SOAPElement) iterator.next();
                if(element.getNodeName().equals("ns:PaymentSysXmlDataResponse")) {
                    Iterator<SOAPElement> iterator_out = element.getChildElements();
                    while(iterator_out.hasNext()){
                        SOAPElement element_out = (SOAPElement) iterator_out.next();
                        if(element_out.getNodeName().equals("ns:return")) {
                            rcvxml = element_out.getValue();
                        }
                    }
                }
            }
            return rcvxml;
        }
        catch(Exception e){
            throw new RuntimeException(BytterConst.SYSTEMNAME + e);
        }
        finally{
            if(connection != null) {
                connection.disconnect();
            }

            try{
                if(null != is) {
                    is.close();
                }
                if(null != os) {
                    os.close();
                }
            }
            catch(IOException e){
                throw new RuntimeException(BytterConst.SYSTEMNAME + e);
            }
        }
    }
复制代码

 

posted @   O-ll-O  阅读(581)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· .NET Core 中如何实现缓存的预热?
· 三行代码完成国际化适配,妙~啊~
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
点击右上角即可分享
微信分享提示