Android, sending XML via HTTP POST (SOAP)
- 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.
- Wrap this string in a StringEntity and set its content type as text/xml
- 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 :).