用java访问c# webservice时,和平时java访问自己不大一样。
try {
// 创建一个服务(service)调用(call)
org.apache.axis.client.Service service = new org.apache.axis.client.Service();
Call call = (Call) service.createCall();// 通过service创建call对象
// 设置service所在URL
call.setTargetEndpointAddress(new java.net.URL(endpoint));
// 方法名(processService)与MyService.java方法名保持一致
call.setOperationName(new QName("http://tempuri.org/", "GetWxyt"));
call.setUseSOAPAction(true);
call.setSOAPActionURI("http://tempuri.org/GetWxyt");
call.addParameter(new QName("http://tempuri.org/", "ytTypeId"),
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.addParameter(new QName("http://tempuri.org/", "ytStartTime"),
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.addParameter(new QName("http://tempuri.org/", "ytEndTime"),
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_SCHEMA);
// ytTypeId ytStartTime ytEndTime
Object object = call.invoke(new Object[] { "1",
"2013-08-30 08:30:00", "2013-08-30 18:30:00" });
System.out.println(object);
//c#返回值的dataset类型,必须进行解析,下面是解析过程
Schema schema = (Schema)object;
//DefaultTableModel model=new DefaultTableModel(new String[]{"strFilePath","strMyImageBytes"},0);
schema.get_any()[1].getChildNodes().getLength();
int nLength=schema.get_any()[1].getChildNodes().item(0).getChildNodes().getLength();
String strFilePath="";
String strMyImageBytes="";
JSONArray jsons = new JSONArray();
for(int i=0;i<nLength;i++)
{
JSONObject jsonObject = new JSONObject();
if(schema.get_any()[1].getChildNodes().item(0).getChildNodes().item(i).getChildNodes().item(0).getNodeName().equals("strFilePath"))
{
strFilePath=schema.get_any()[1].getChildNodes().item(0).getChildNodes().
item(i).getChildNodes().item(0).getFirstChild().getNodeValue();
jsonObject.put("strFilePath", strFilePath);
}
if(schema.get_any()[1].getChildNodes().item(0).getChildNodes().item(i).getChildNodes().item(1).getNodeName().equals("strMyImageBytes"))
{
strMyImageBytes=schema.get_any()[1].getChildNodes().item(0).
getChildNodes().item(i).getChildNodes().item(1).getFirstChild().getNodeValue();
jsonObject.put("strMyImageBytes", strMyImageBytes);
}
//model.addRow(new String[]{strFilePath,strMyImageBytes});
jsons.add(jsonObject);
}
System.out.println(jsons.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
}
其中有几个设置是不可缺少的
call.setOperationName(new QName("http://tempuri.org/", "GetWxyt"));
call.setUseSOAPAction(true);
call.setSOAPActionURI("http://tempuri.org/GetWxyt");
http://tempuri.org/是wsdl:definitions结点下的targetNamespace结点的属性值。
call.addParameter(new QName("http://tempuri.org/", "ytTypeId"),
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.addParameter(new QName("http://tempuri.org/", "ytStartTime"),
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.addParameter(new QName("http://tempuri.org/", "ytEndTime"),
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_SCHEMA);
new QName("http://tempuri.org/", "ytEndTime")这边参数也必须加上命名空间,否则会出现莫名其妙的错误。
最后面是dataset类型的返回值的获取,可以参考下。