Guest User

Untitled

a guest
Jul 16th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. package de.androbit.test.soap;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5.  
  6. import org.apache.http.HttpEntity;
  7. import org.apache.http.HttpResponse;
  8. import org.apache.http.auth.AuthScope;
  9. import org.apache.http.auth.UsernamePasswordCredentials;
  10. import org.apache.http.client.methods.HttpPost;
  11. import org.apache.http.entity.FileEntity;
  12. import org.apache.http.impl.client.DefaultHttpClient;
  13. import org.apache.http.util.EntityUtils;
  14.  
  15. public class ServiceClient {
  16. public static final String encoding = "UTF-8";
  17.  
  18. private String hostname;
  19. private int port;
  20.  
  21. private String username;
  22. private String password;
  23.  
  24. public ServiceClient(String hostname, int port, String username,
  25. String password) {
  26. super();
  27. this.hostname = hostname;
  28. this.port = port;
  29. this.username = username;
  30. this.password = password;
  31. }
  32.  
  33. public String invokeWebService(String webServiceURL,
  34. String requestXMLPath) throws FileNotFoundException, Exception {
  35. DefaultHttpClient httpclient = new DefaultHttpClient();
  36. try {
  37. httpclient.getCredentialsProvider().setCredentials(new AuthScope(hostname, port),
  38. new UsernamePasswordCredentials(username, password));
  39.  
  40. FileEntity reqEntity = new FileEntity(new File(requestXMLPath) , "text/xml");
  41. reqEntity.setContentEncoding(encoding);
  42.  
  43. HttpPost httpPost = new HttpPost(webServiceURL);
  44. httpPost.addHeader("SOAPAction", "");
  45. httpPost.setEntity(reqEntity);
  46.  
  47. HttpResponse response = httpclient.execute(httpPost);
  48. HttpEntity entity = response.getEntity();
  49.  
  50. if (entity != null) {
  51. System.out.println("Response content length: " + entity.getContentLength());
  52. String responseString = EntityUtils.toString(entity);
  53. entity.consumeContent();
  54. return responseString;
  55. }
  56. }
  57. finally {
  58. // shutdown connection
  59. httpclient.getConnectionManager().shutdown();
  60. }
  61. return null;
  62. }
  63. }
Add Comment
Please, Sign In to add comment