http client method to call webservice

For beginners, it does not seem to be very simple to assemble soap request packets, but there is a simple way to obtain soap packets, that is, through the soapui plug-in, you can get the request packets. Learn more about soapui, and I won't go into details here. The above code

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Call Webservice service through UrlConnection
*
*/
public class HttpClientTest {

    public static void main (String[] args) throws Exception {
        //Service address
        URL wsUrl = new URL("http://localhost:8080/server/plus?wsdl");
       
        HttpURLConnection conn = (HttpURLConnection) wsUrl.openConnection();
       
        conn .setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
       
        OutputStream os = conn.getOutputStream();
       
       
        String soap = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://server/\">"+
        "<soapenv:Header/>"+
        "<soapenv:Body>"+
        "<ser:add>"+
        "<arg0>5</arg0>"+
        "<arg1>6</arg1>"+
        "</ser:add>"+
        "</soapenv:Body>"+
        "</soapenv:Envelope>";
       
        os.write(soap.getBytes());
        InputStream is = conn.getInputStream();
       
        byte[] b = new byte[1024];
        int len = 0;
        String s = "";
        while((len = is.read(b)) != -1){
            String ss = new String(b,0,len,"UTF-8");
            s += ss;
        }
        System.out.println(s);
       
        is.close();
        os.close();
        conn.disconnect();
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327072035&siteId=291194637