7/15/08


Apache AXIS

Simple invoke standard Version web service via URLConnection:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class Main {

public static void main(String[] args) throws Exception {

HttpURLConnection httpurlconnection = null;
String HostUrl = "localhost";
String SoapActionUrl = "http://localhost:8080/axis/services/Version";

URL url = new URL(SoapActionUrl);
URLConnection urlconnection = url.openConnection();
httpurlconnection = (HttpURLConnection) urlconnection;

StringBuffer stringbuffer = new StringBuffer();

stringbuffer.append("<SOAP-ENV:Envelope xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
stringbuffer.append("<SOAP-ENV:Body>");
stringbuffer.append("<getVersion>");
stringbuffer.append("</getVersion></SOAP-ENV:Body></SOAP-ENV:Envelope>");

byte abyte0[] = stringbuffer.toString().getBytes();
httpurlconnection.addRequestProperty("Host", HostUrl);
httpurlconnection.addRequestProperty("Content-Length", String.valueOf(abyte0.length));
httpurlconnection.addRequestProperty("Content-Type", "text/xml; charset=UTF-8");
httpurlconnection.addRequestProperty("SOAPAction", SoapActionUrl);
httpurlconnection.setInstanceFollowRedirects(true);

httpurlconnection.setRequestMethod("POST");
httpurlconnection.setDoOutput(true);
httpurlconnection.setDoInput(true);

OutputStream outputstream = httpurlconnection.getOutputStream();
outputstream.write(abyte0);
outputstream.close();

System.out.println(httpurlconnection.getResponseMessage());

InputStreamReader inputstreamreader = new InputStreamReader(httpurlconnection.getInputStream());
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
StringBuffer stringbuffer1 = new StringBuffer();
String s3;
while ((s3 = bufferedreader.readLine()) != null) {
stringbuffer1.append(s3);
System.out.println(s3);
}
bufferedreader.close();
httpurlconnection = null;
}
}

Result:

OK
<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><getVersionResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><getVersionReturn xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)</getVersionReturn></getVersionResponse></soapenv:Body></soapenv:Envelope>

Request

POST /axis/services/Version HTTP/1.1
Host: localhost
Content-Length: 253
Content-Type: text/xml; charset=UTF-8
SOAPAction: http://localhost:8080/axis/services/Version
User-Agent: Java/1.4.2
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive

<SOAP-ENV:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><SOAP-ENV:Body><getVersion></getVersion></SOAP-ENV:Body></SOAP-ENV:Envelope>

Response

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/xml;charset=utf-8
Transfer-Encoding: chunked
Date: Tue, 15 Jul 2008 11:41:14 GMT

<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><getVersionResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><getVersionReturn xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)</getVersionReturn></getVersionResponse></soapenv:Body></soapenv:Envelope>


Using AXIS classes:

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;

public class Main {
public static void main(String[] args) throws Exception {
String endpoint = "http://localhost:8080/axis/services/Version";
Call call = (Call) new Service().createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperationName(new QName("", "getVersion"));
String ret = (String) call.invoke(new Object[] { });
System.out.println(ret);
}
}

Result:

Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)

Request:

POST /axis/services/Version HTTP/1.0
Content-Type: text/xml; charset=utf-8
Accept: application/soap+xml, application/dime, multipart/related, text/*
User-Agent: Axis/1.4
Host: localhost:8080
Cache-Control: no-cache
Pragma: no-cache
SOAPAction: ""
Content-Length: 340

<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><getVersion soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/></soapenv:Body></soapenv:Envelope>

Response:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/xml;charset=utf-8
Date: Tue, 15 Jul 2008 10:35:47 GMT
Connection: close

<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><getVersionResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><getVersionReturn xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)</getVersionReturn></getVersionResponse></soapenv:Body></soapenv:Envelope>

No comments: