Friday, 12 October 2018

Java + SOAP API

<dependency>
<groupId>javax.xml.soap</groupId>
<artifactId>javax.xml.soap-api</artifactId>
<version>1.3.8</version>
</dependency>


package com.google.soap;

import java.io.ByteArrayInputStream;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.util.Base64;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

public class Final {
public static void main(String[] args) throws SOAPException {
String str = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/ </soapenv:Body></soapenv:Envelope>";
SOAPConnection connection = null;
String authorization = Base64.getEncoder()
.encodeToString(("uname" + ":" + "pwd").getBytes(Charset.forName("UTF-8")));
String endPointURLString = "http://googlec.om/abc";

try {
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
connection = soapConnectionFactory.createConnection();
MessageFactory factory = MessageFactory.newInstance();

SOAPMessage request = factory.createMessage(new MimeHeaders(), new ByteArrayInputStream(str.getBytes()));

MimeHeaders headers = request.getMimeHeaders();
headers.addHeader("Authorization", "Basic " + authorization);
headers.addHeader("SOAPAction", "getSum");
headers.addHeader("Content-Type", "multipart/related; type=\"text/xml\";");
request.saveChanges();
SOAPMessage response = connection.call(request, endPointURLString);
System.out.println(getResspString(response));

} catch (Exception ex) {
System.out.println(ex.getMessage());
} finally {
connection.close();
}
}

public static String getResspString(SOAPMessage response) {
final StringWriter sw = new StringWriter();
try {
TransformerFactory.newInstance().newTransformer().transform(new DOMSource(response.getSOAPPart()),
new StreamResult(sw));
} catch (TransformerException e) {
throw new RuntimeException(e);
}
return sw.toString();
}
}

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) {
}
}