zoukankan      html  css  js  c++  java
  • 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);
        }
    }

  • 相关阅读:
    mysql 为表添加索引
    南开大学2021年高等代数考研试题参考解答
    南开大学2021年数学分析考研试题参考解答
    中国人民大学2021年数学分析考研试题参考解答
    河北工业大学2021数分高代考研
    西南大学2021高等代数考研
    西南大学2021数学分析考研
    重庆大学2021数学分析考研
    [数学考研竞赛00076]武汉大学2021年高等代数考研试题参考解答
    华东师范大学2021年高等代数考研试题参考解答
  • 原文地址:https://www.cnblogs.com/zhang-boke/p/5439605.html
Copyright © 2011-2022 走看看