Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. public static WebServiceResp makeWebServiceCall(String XML, String urlPath) throws IOException{
  2. //Code to make a web service HTTP request
  3. String responseString = "";
  4. String outputString = "";
  5. String wsURL = urlPath;
  6. URL url = new URL(wsURL);
  7. URLConnection connection = url.openConnection();
  8. HttpsURLConnection httpConn = (HttpsURLConnection)connection;
  9. ByteArrayOutputStream bout = new ByteArrayOutputStream();
  10.  
  11.  
  12. //System.out.println(XML);
  13.  
  14. byte[] buffer = new byte[XML.length()];
  15. buffer = XML.getBytes();
  16. bout.write(buffer);
  17. byte[] b = bout.toByteArray();
  18. // Set the appropriate HTTP parameters.
  19. httpConn.setRequestProperty("Content-Length",
  20. String.valueOf(b.length));
  21. httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
  22. httpConn.setRequestMethod("POST");
  23. httpConn.setRequestProperty("Cache-Control", "no-cache");
  24.  
  25. httpConn.setDoOutput(true);
  26. httpConn.setDoInput(true);
  27.  
  28.  
  29. OutputStream out = httpConn.getOutputStream();
  30. //Write the content of the request to the outputstream of the HTTP Connection.
  31. out.write(b);
  32. out.close();
  33. //Ready with sending the request.
  34.  
  35. //Check the status
  36. int status = httpConn.getResponseCode();
  37. Log.d(TAG, "makeWebServiceCall: "+"Processing Status: "+status);
  38.  
  39.  
  40. BufferedReader in;
  41. if (status <= 200) {
  42. //Read the response.
  43. Log.d(TAG, "makeWebServiceCall: Getting Input Stream");
  44. InputStreamReader isr =
  45. new InputStreamReader(httpConn.getInputStream());
  46. in = new BufferedReader(isr);
  47. }else{
  48. //Read the response.
  49. Log.d(TAG, "makeWebServiceCall: Getting Error Stream");
  50. InputStreamReader isr =
  51. new InputStreamReader(httpConn.getErrorStream());
  52. in = new BufferedReader(isr);
  53. }
  54.  
  55. //Write the SOAP message response to a String.
  56. while ((responseString = in.readLine()) != null) {
  57. outputString = outputString + responseString;
  58. }
  59. Log.d(TAG, "makeWebServiceCall: WebServiceResponse " + outputString);
  60. //Parse the String output to a org.w3c.dom.Document and be able to reach every node with the org.w3c.dom API.
  61. Document document = Utils.parseXmlFile(outputString);
  62. //NodeList nodeLst = document.getElementsByTagName("GetWeatherResult");
  63. // String weatherResult = nodeLst.item(0).getTextContent();
  64. //System.out.println("Weather: " + weatherResult);
  65.  
  66. //Write the SOAP message formatted to the console.
  67.  
  68. WebServiceResp webServiceResp = new WebServiceResp();
  69. webServiceResp.setDocument(document);
  70. webServiceResp.setStatus(status);
  71. return webServiceResp;
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement