• HttpURLConnection 直接发送soap消息调用webservice




    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.InetSocketAddress;
    import java.net.Proxy;
    import java.net.URL;


    public class Test {


        private String testConn(String xml) {
            String urlString = "http://。。。。。WebService.asmx";
            HttpURLConnection httpConn = null;
            OutputStream out = null;
            String returnXml = "";
            System.out.println("开始.......");
            try {
                Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("IP", 端口));
                
                httpConn = (HttpURLConnection) new URL(urlString).openConnection(proxy);
                httpConn.setRequestProperty("Content-Type","text/xml; charset=utf-8");
                httpConn.setRequestProperty("soapActionString", "http://tempuri.org/_3uffpWS/_3uffpWebService/....");
                httpConn.setRequestMethod("POST");
                httpConn.setDoOutput(true);
                httpConn.setDoInput(true);
                httpConn.connect();
                out = httpConn.getOutputStream(); // 获取输出流对象
                httpConn.getOutputStream().write(xml.getBytes()); // 将要提交服务器的SOAP请求字符流写入输出流
                out.flush();
                out.close();
                int code = httpConn.getResponseCode(); // 用来获取服务器响应状态
                String tempString = null;
                StringBuffer sb1 = new StringBuffer();
                System.out.println("code="+code);
                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();
                System.out.println("结束.......");
            } catch ( Exception e ) {
                e.printStackTrace();
            }
            return returnXml;
        }
        

        public static void main(String[] args) {

            String xml = "<?xml version="1.0" encoding="utf-8"?>"
                +"<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/">"
                +"<soap:Body>"
                +"<ReturnSmsValidateCode xmlns="http://tempuri.org/.......S/...WebService">"
                +"<memberID>string</memberID>"
                +"<validateNumUserInput>155555555555555</validateNumUserInput>"
                +"</ReturnSmsValidateCode>"
                +"</soap:Body>"
                +"</soap:Envelope>";
            
            String r = new Test().testConn(xml);
            System.out.println(r);
        }
    }

  • 相关阅读:
    2020年终总结
    vi编辑器使用基本操作
    为什么Windows下重载Nginx配置不生效
    JAVA、C#中使用正则表达式替换字符串
    领域事件、集成事件、事件总线区别与关系
    asp.net core中使用Serilog以及自定义Enricher
    C语言----文件(高阶篇二十八)
    C语言----指针与一维数组(高阶篇二十七)
    C语言----位运算(进阶篇二十六)
    C语言----指针基础(进阶篇二十五)
  • 原文地址:https://www.cnblogs.com/zhang-boke/p/5439605.html
Copyright © 2020-2023  润新知