JAVA调用.NET的webservice

webservice信息
POST /upic.asmx HTTP/1.1
Host: u.domain.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/UploadFile"

<?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>
    <UploadFile xmlns="http://tempuri.org/">
      <fs>base64Binary</fs>
      <FilePath>string</FilePath>
      <FileName>string</FileName>
      <isCheckFileExist>boolean</isCheckFileExist>
    </UploadFile>
  </soap:Body>
</soap:Envelope>



需要的包:axis.jar,axis-ant.jar,commons-logging-1.1.jar,commons-discovery-0.2.jar,jaxrpc.jar,wsdl4j.jar

代码
public String getNetService(String fs,String FilePath,String FileName,boolean isCheckFileExist) {
		try{
			String picUrl="http://u.domain.com/upic.asmx";
			String SOAPACTION="http://u.domain.com/";
			String methodName="UploadFile";//Web 服务提供的方法
			Service service = new Service(); 
			Call call = (Call)service.createCall();
			call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP12_CONSTANTS);
			call.setTargetEndpointAddress(new URL(picUrl));
			call.setOperationName(new QName("http://tempuri.org/", methodName));
			call.addParameter(new QName("http://tempuri.org/", "fs"),org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);//定义参数
			call.addParameter(new QName("http://tempuri.org/", "FilePath"),org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
			call.addParameter(new QName("http://tempuri.org/", "FileName"),org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
			call.addParameter(new QName("http://tempuri.org/", "isCheckFileExist"),org.apache.axis.encoding.XMLType.XSD_BOOLEAN,javax.xml.rpc.ParameterMode.IN);
			call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
			call.setUseSOAPAction(true);
			call.setSOAPActionURI(SOAPACTION + methodName);			
			String res = (String) call.invoke(new Object[] { fs, FilePath,FileName, isCheckFileExist});//按定义的顺序给参数传值
			return res;
		}catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
	}

猜你喜欢

转载自hq369.iteye.com/blog/1901751