java implementation of post request web service server

 

When the client that generates the webService is not easy to implement, the post request through java is a good way.

1. For example, I have a wsdl file at this time

http://xxx.xxx.xxx.xxx:8081/APIService.svc?wsdl

 

  public static void main(String[] args) {
        try{
            String url="http://xxx.xxx.xxx.xxx:8081/APIService.svc";
            String params="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\" xmlns:hn=\"http://schemas.datacontract.org/2004/07/hn.DataAccess.bll\">\n" +
                    "         </tem:input>\n" +
                    "   </soapenv:Body>\n" +
                    "</soapenv:Envelope>";
            String host="xxx.xxx.xxx.xxx:8081";
            String SOAPAction="http://aaa/aaa/aaa";

            //Accept return message
            String result = new String();
            URL u = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) u.openConnection();
            conn.setDoInput(true);
            //Allow data export
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setDefaultUseCaches(false);
            conn.setRequestProperty("Host",host);
            conn.setRequestProperty("Content-Type","text/xml;charset=UTF-8");
            //soap
            conn.setRequestProperty("SOAPAction",SOAPAction);
            conn.setRequestProperty("Content-Length",String.valueOf(params.length()));
            conn.setRequestMethod("POST");
            //Define output flow
            OutputStream output = conn.getOutputStream();
            if(StringUtils.isNotBlank(params)){
                byte[] b = params.getBytes("UTF-8");
                //Send soap request message
                output.write(b,0,b.length);
                output.flush();
                output.close();
                //Define the input stream and obtain the soap message
                InputStream input = conn.getInputStream();
                //Set encoding format
                result = IOUtils.toString(input,"UTF-8");
                input.close();
            }
            System.out.println("Request return message:" + result);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }

    }

2. Parameter Description:

url: This is the address of the wsdl file. Remove the suffix "? wsdl". For example:

wsdl:

http://xxx.xxx.xxx.xxx:8081/APIService.svc?wsdl

url:

http://xxx.xxx.xxx.xxx:8081/APIService.svc

params: This is the request parameter

Host: This is the host address and port number of the other party. For example: xxx.xxx.xxx.xxx:8081

SOAPAction: specific service path

3. Through SoapUI, we can convert swdl files. To simulate sending a request. And parameters

Click row to view specific sending parameters

 

Specific parameter corresponding rules

 

Tags: Java xml encoding

Posted on Fri, 01 Nov 2019 15:22:40 -0700 by p.persia69