Android, sending XML via HTTP POST (SOAP)

  1. First, you can create a String template for this SOAP request and substitute user-supplied values at runtime in this template to create a valid request.
  2. Wrap this string in a StringEntity and set its content type as text/xml
  3. Set this entity in the SOAP request.

Something like:

HttpPost httppost = new HttpPost(SERVICE_EPR);          
StringEntity se = new StringEntity(SOAPRequestXML,HTTP.UTF_8);

se.setContentType("text/xml");  
httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8");
httppost.setEntity(se);  

HttpClient httpclient = new DefaultHttpClient();
BasicHttpResponse httpResponse = 
    (BasicHttpResponse) httpclient.execute(httppost);

response.put("HTTPStatus",httpResponse.getStatusLine().toString());

here the alternative to send soap msg.

public String setSoapMsg(String targetURL, String urlParameters){

        URL url;
        HttpURLConnection connection = null;  
        try {
          //Create connection
          url = new URL(targetURL);

         // for not trusted site (https)
         // _FakeX509TrustManager.allowAllSSL();
         // System.setProperty("javax.net.debug","all");

          connection = (HttpURLConnection)url.openConnection();
          connection.setRequestMethod("POST");


          connection.setRequestProperty("SOAPAction", "**** SOAP ACTION VALUE HERE ****");

          connection.setUseCaches (false);
          connection.setDoInput(true);
          connection.setDoOutput(true);


          //Send request
          DataOutputStream wr = new DataOutputStream (
                       connection.getOutputStream ());
          wr.writeBytes (urlParameters);
          wr.flush ();
          wr.close ();

          //Get Response    
          InputStream is ;
          Log.i("response", "code="+connection.getResponseCode());
          if(connection.getResponseCode()<=400){
              is=connection.getInputStream();
          }else{
              /* error from server */
              is = connection.getErrorStream();
        } 
         // is= connection.getInputStream();
          BufferedReader rd = new BufferedReader(new InputStreamReader(is));
          String line;
          StringBuffer response = new StringBuffer(); 
          while((line = rd.readLine()) != null) {
            response.append(line);
            response.append('\r');
          }
          rd.close();
          Log.i("response", ""+response.toString());
          return response.toString();

        } catch (Exception e) {

         Log.e("error https", "", e);
          return null;

        } finally {

          if(connection != null) {
            connection.disconnect(); 
          }
        }
      }

hope it helps. if anyone wonder allowAllSSL() method, google it :).


So if you use:

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

It is still rest, but if you use:

StringEntity se = new StringEntity(SOAPRequestXML,HTTP.UTF_8);
httppost.setEntity(se);  

It is soap???


Example sending XML to WS via http POST.

DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://foo/service1.asmx/GetUID");     

        //XML example to send via Web Service.
        StringBuilder sb = new StringBuilder();
        sb.append("<myXML><Parametro><name>IdApp</name><value>1234567890</value></Parameter>");
        sb.append("<Parameter><name>UID1</name><value>abc12421</value></Parameter>");
                sb.append("</myXML>");

        httppost.addHeader("Accept", "text/xml");
        httppost.addHeader("Content-Type", "application/x-www-form-urlencoded");

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
        nameValuePairs.add(new BasicNameValuePair("myxml", sb.toString());//WS Parameter and    Value           
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);

Here's my code for sending HTML.... You can see the data is the nameValuePairs.add(...)

        HttpClient httpclient = new DefaultHttpClient();
        // Your URL
        HttpPost httppost = new HttpPost("http://192.71.100.21:8000");

        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            // Your DATA
            nameValuePairs.add(new BasicNameValuePair("id", "12345"));
            nameValuePairs.add(new BasicNameValuePair("stringdata","AndDev is Cool!"));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response;
            response = httpclient.execute(httppost);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }