How to call a SOAP webservice with a simple String (xml in string format)

前端 未结 3 2108
野趣味
野趣味 2021-01-06 07:28

I have this string representing a XML:

String soapCall=\"

        
3条回答
  •  臣服心动
    2021-01-06 07:40

    Solution:

    private byte[] callSOAPServer() {
    
        byte[] result = null;
    
        HttpParams httpParameters = new BasicHttpParams();
        // Set the timeout in milliseconds until a connection is established.
        int timeoutConnection = 15000;
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
        // Set the default socket timeout (SO_TIMEOUT)
        // in milliseconds which is the timeout for waiting for data.
        int timeoutSocket = 35000;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
    
        DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);
    
        /*
         * httpclient.getCredentialsProvider().setCredentials( new
         * AuthScope("os.icloud.com", 80, null, "Digest"), new
         * UsernamePasswordCredentials(username, password));
         */
        HttpPost httppost = new HttpPost(SERVER_URL );
        httppost.setHeader("soapaction", SOAP_ACTION);
        httppost.setHeader("Content-Type", "text/xml; charset=utf-8");
    
        System.out.println("executing request" + httppost.getRequestLine());
       //now create a soap request message as follows:
        final StringBuffer soap = new StringBuffer();
        soap.append("\n");
        soap.append("");
       // this is a sample data..you have create your own required data  BEGIN
        soap.append(" \n");
        soap.append(" \n");
        soap.append("" + body);
        soap.append(" \n");
        soap.append(" \n");
    
        /* soap.append(body); */
         // END of MEssage Body
        soap.append("");
        Log.i("SOAP Request", ""+soap.toString());
       // END of full SOAP request  message
        try {
            HttpEntity entity = new StringEntity(soap.toString(),HTTP.UTF_8);
            httppost.setEntity(entity); 
            HttpResponse response = httpclient.execute(httppost);// calling server
            HttpEntity r_entity = response.getEntity();  //get response
            Log.i("Reponse Header", "Begin...");          // response headers
            Log.i("Reponse Header", "StatusLine:"+response.getStatusLine());
            Header[] headers = response.getAllHeaders();
            for(Header h:headers){
                Log.i("Reponse Header",h.getName() + ": " + h.getValue());
            }
            Log.i("Reponse Header", "END...");
            if (r_entity != null) {       
                result = new byte[(int) r_entity.getContentLength()];  
                if (r_entity.isStreaming()) {
                    DataInputStream is = new DataInputStream(
                            r_entity.getContent());
                    is.readFully(result);
                }
            }
        } catch (Exception E) {
            Log.i("Exception While Connecting", ""+E.getMessage());
            E.printStackTrace();
        }
    
        httpclient.getConnectionManager().shutdown(); //shut down the connection
        return result;
       }
    

    2) You have to parse the output of above function returned byteArray. For example:

    byte[] initReqrepsonse = callSOAPServer(soapBodymessage );
    ByteArrayInputStream bais=new ByteArrayInputStream(initReqrepsonse);
    // now parse the xml as
    /** Handling XML */
    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser sp = spf.newSAXParser();
    XMLReader xr = sp.getXMLReader();
    
    /** Create handler to handle XML Tags ( extends DefaultHandler ) */
    // ResponseParser  is XML parser class which will parse the XML output.
    ResponseParser myXMLHandler = new ResponseParser();
    xr.setContentHandler(myXMLHandler);
    Log.i("XML data", bais.toString());
    xr.parse(new InputSource(bais));
    

    This way,you can access Any SOAP webservice methods without third-party libraries. Please let me know if any corrections are required.

提交回复
热议问题