JDK5.0以后,建立web服务就变得非常方便。不论是xfire的功能强大,还是netbeans的快捷,都给我留下深刻的印象。然而,在JDK1.4下就没有这么方便了。尤其体现在使用java以外的语言生成服务端的时候。
下面是JDK1.4下使用axis访问.net服务端的代码,也是网上收罗来的,链接已经找不到了。
import
javax.xml.namespace.QName;
import
javax.xml.rpc.ParameterMode;
import
org.apache.axis.client.Call;
import
org.apache.axis.client.Service;
import
org.apache.axis.encoding.XMLType;

public
class
AxisDemo
...
{

public static void main(String[] args) ...{

try ...{
String endpoint = "http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
QName qn = new QName("http://WebXml.com.cn/", "qqCode");
call.addParameter(qn, XMLType.XSD_STRING,ParameterMode.IN);
call.setReturnType(XMLType.XSD_STRING);
call.setUseSOAPAction(true);
call.setSOAPActionURI("http://WebXml.com.cn/qqCheckOnline");
call.setOperationName(new QName("http://WebXml.com.cn/", "qqCheckOnline"));
String qqCd = "32083343";
String ret = (String) call.invoke(new Object[] ...{ qqCd });
System.out.println("Got Result : " + ret);
} catch (Exception e) ...{
System.err.println(e.toString());
}
}
}
其中:
倘若参数如下定义很有可能无法传到.net里去。(服务端java的话似乎没问题)
call.addParameter("qqCode", XMLType.XSD_STRING, ParameterMode.IN);
备注:
new QName("http://www.w3.org/2001/XMLSchema", "float") 和 org.apache.axis.encoding.XMLType.XSD_FLOAT是等效的。
OperationStyle分成:wrapped,document,rpc,message等多种方式 。当调用.net服务端时,一般需要进行设定。
相关资料请参阅:
http://www.ibm.com/developerworks/cn/webservices/ws-soa-intersoap/
http://www.ibm.com/developerworks/cn/webservices/ws-docstyle/
http://dev2dev.bea.com.cn/blog/shallon/200711/30_714.html
如下:
import
javax.xml.namespace.QName;
import
javax.xml.rpc.ParameterMode;
import
org.apache.axis.client.Call;
import
org.apache.axis.client.Service;

public
class
TestClient
...
{

public static void main(String[] args) ...{
try ...{
String endpoint = "http://chs.gotdotnet.com/quickstart/aspplus/samples/services/MathService/VB/MathService.asmx";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
Float i = new Float(1);
Float j = new Float(2);
QName p0 = new QName("http://tempuri.org/", "A");
call.addParameter(p0, new QName("http://www.w3.org/2001/XMLSchema", "float"),ParameterMode.IN);
QName p1 = new QName("http://tempuri.org/", "B");
call.addParameter(p1, org.apache.axis.encoding.XMLType.XSD_FLOAT,ParameterMode.IN);
call.setReturnType(new QName("http://www.w3.org/2001/XMLSchema", "float"));
call.setUseSOAPAction(true);
call.setSOAPActionURI("http://tempuri.org/Add");
call.setEncodingStyle(null);
call.setProperty(org.apache.axis.AxisEngine.PROP_DOMULTIREFS,Boolean.FALSE);
call.setProperty(org.apache.axis.client.Call.SEND_TYPE_ATTR,Boolean.FALSE);
call.setOperationStyle("wrapped");
call.setOperationName(new QName("http://tempuri.org/", "Add"));
Float ret = (Float) call.invoke(new Object[] ...{ i, j });
System.out.println("Got result : " + ret);
} catch (Exception e) ...{
System.err.println(e.toString());
}
}
}
2万+

被折叠的 条评论
为什么被折叠?



