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);
    }
}

发布了1 篇原创文章 · 获赞 2 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/u011927449/article/details/104032930