Java Web Service Client

I'm trying to write a Java client to connect to a web service using Axis 1.4 (choose this by default, perhaps I should use something else?). The web service has a URL of the form "http://domain.com/directory/service.asmx" and I'm trying to call a method of the form MethodName(String stateAbbr). I get an error saying that I'm not providing the parameter, my code is below:

String stateAbbrev = "Tx";

String endpoint = "http://domain.com/directory/service.asmx";

Service service = new Service();

Call call = (Call)service.createCall();

call.setTargetEndpointAddress(new URL(endpoint));

call.setSOAPActionURI("MYSOAPACTIONURI");

call.setOperationName(new QName(URI, MethodName));

call.addParameter("stateAbbr", XMLType.XSD_STRING, ParameterMode.IN);

call.setReturnType(XMLType.XSD_ANY);

call.invoke(new Object[] {stateAbbrev});

[896 byte] By [EdLaFavea] at [2007-11-27 11:49:34]
# 1

r u trying to send a string or a soap envelope?

JWKC-5ivea at 2007-7-29 18:26:04 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

I guess that I'm trying to send an envelope. The only instruction I have is that the request is supposed to look like this:

<?xml version="1.0" encoding="utf-8"?>

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

<soap:Body>

<METHODNAME xmlns="TARGETNAMESPACE">

<PARAMETER>string</PARAMETER>

<PARAMETER2>string</PARAMETER2>

</METHODNAME>

</soap:Body>

</soap:Envelope>

I tried the following code to build the soap envelop directly but I had the same error with it:

Service service = new Service();

Call call = (Call)service.createCall();

call.setTargetEndpointAddress(endpoint);

call.setSOAPActionURI(targetNameSpace + operationName);

call.setUseSOAPAction(true);

call.setOperationName(new QName(targetNameSpace, operationName));

SOAPEnvelope envelope = new SOAPEnvelope();

SOAPBody body = envelope.getBody();

SOAPElement methodElement = body.addChildElement( envelope.createName(operationName, null, targetNameSpace));

SOAPElement countyElement = methodElement.addChildElement( "PARAMETER" );

countyElement.addTextNode(PARAMETERValue);

SOAPElement stateElement = methodElement.addChildElement( "PARAMETER2" );

stateElement.addTextNode(PARAMETER2Value);

SOAPEnvelope responseEnvelope = call.invoke(envelope);

Ultimately I was able to write code using a HttpURLConnection to directly post data to the web service and read the response, however I'm still curious how you'd do this with the SOAP stuff.

EdLaFavea at 2007-7-29 18:26:04 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

try the following:

ServiceClient client = new ServiceClient();

OperationClient operationClient = client.createClient(ServiceClient.ANON_OUT_IN_OP);

MessageContext outMsgCtx = new MessageContext();

Options opts = outMsgCtx.getOptions();

opts.setTo(new EndpointReference(endpoint);

opts.setAction(action);

outMsgCtx.setEnvelope(yourEnvelope);

operationClient.addMessageContext(outMsgCtx);

operationClient.execute(true);

MessageContext inMsgtCtx = operationClient.getMessageContext("In");

SOAPEnvelope response = inMsgtCtx.getEnvelope();

System.out.println(response);

JWKC-5ivea at 2007-7-29 18:26:04 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...