HttpURLConnection directly send soap messages to call 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 (the HttpURLConnection) new new the URL (the urlString) .OpenConnection (Proxy);
            httpConn.setRequestProperty ( "the Content-the Type", "text / XML; charset = UTF-. 8");
            httpConn.setRequestProperty ( "soapActionString", "HTTP: //tempuri.org/_3uffpWS/_3uffpWebService / .... ");
            httpConn.setRequestMethod (" the POST ");
            httpConn.setDoOutput (to true);
            httpConn.setDoInput (to true);
            httpConn.connect ();
            OUT = httpConn. getOutputStream (); // obtain an output stream object
            httpConn.getOutputStream () write (xml.getBytes () );. // to be submitted to the server SOAP request character stream to the output stream
            out.flush ();
            the out.close () ;
            int = httpConn.getResponseCode code ();// used to obtain the server response status
            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"));
                // read into one line, until the end of the file is read into the null
                the while {((reader.readLine tempString = ()) = null!)
                    Sb1.append (tempString);
                }
                IF (! = Null Reader) {
                    reader.Close ( );
                }
            }
            // response packet
            returnXml sb1.toString = ();
            System.out.println ( "end .......");
        } the 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);
    }
}

Published an original article · won praise 2 · views 10000 +

Guess you like

Origin blog.csdn.net/u011927449/article/details/104032930