Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.androbit.test.soap;
- import java.io.File;
- import java.io.FileNotFoundException;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.auth.AuthScope;
- import org.apache.http.auth.UsernamePasswordCredentials;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.entity.FileEntity;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.util.EntityUtils;
- public class ServiceClient {
- public static final String encoding = "UTF-8";
- private String hostname;
- private int port;
- private String username;
- private String password;
- public ServiceClient(String hostname, int port, String username,
- String password) {
- super();
- this.hostname = hostname;
- this.port = port;
- this.username = username;
- this.password = password;
- }
- public String invokeWebService(String webServiceURL,
- String requestXMLPath) throws FileNotFoundException, Exception {
- DefaultHttpClient httpclient = new DefaultHttpClient();
- try {
- httpclient.getCredentialsProvider().setCredentials(new AuthScope(hostname, port),
- new UsernamePasswordCredentials(username, password));
- FileEntity reqEntity = new FileEntity(new File(requestXMLPath) , "text/xml");
- reqEntity.setContentEncoding(encoding);
- HttpPost httpPost = new HttpPost(webServiceURL);
- httpPost.addHeader("SOAPAction", "");
- httpPost.setEntity(reqEntity);
- HttpResponse response = httpclient.execute(httpPost);
- HttpEntity entity = response.getEntity();
- if (entity != null) {
- System.out.println("Response content length: " + entity.getContentLength());
- String responseString = EntityUtils.toString(entity);
- entity.consumeContent();
- return responseString;
- }
- }
- finally {
- // shutdown connection
- httpclient.getConnectionManager().shutdown();
- }
- return null;
- }
- }
Add Comment
Please, Sign In to add comment