Thursday, 11 October 2018

JAVA + SOAP API CALL

<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>

public static void main2(String[] args) {
try {
HttpClient client = new HttpClient();
HttpState state = client.getState();
Credentials credentials = new UsernamePasswordCredentials("uname", "pwd");
state.setCredentials(null, null, credentials);

String strXMLFilename = "C:\\Users\\KKasthuri\\Documents\\req.xml";
File input = new File(strXMLFilename);
RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");

String url = "http://abc.com/xyx/proj";
HttpMethod method = new PostMethod(url);
method.setRequestHeader("SOAPAction", "getmthods");
((EntityEnclosingMethod) method).setRequestEntity(entity);

client.executeMethod(method);
String response = method.getResponseBodyAsString();

System.out.println(response);
method.releaseConnection();
} catch (Exception ex) {
}
}



public static void main4(String[] args) {
try {
HttpClient client = new HttpClient();
HttpState state = client.getState();
Credentials credentials = new UsernamePasswordCredentials("uname", "pwd");
state.setCredentials(null, null, credentials);

String url = "http://google.com/test";
HttpMethod method = new PostMethod(url);
method.setRequestHeader("SOAPAction", "MyMethod");
method.setRequestHeader("Content-Type", "text/xml");
((EntityEnclosingMethod) method).setRequestEntity(new StringRequestEntity(
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:v1=\"http://yieldstar.com/ws/AppExchange/v1\"><soapenv:Header/><soapenv:Body></soapenv:Body></soapenv:Envelope>"));

client.executeMethod(method);
String response = method.getResponseBodyAsString();

System.out.println(response);
method.releaseConnection();
} catch (Exception ex) {
}
}