ITeye博客

java通过axis访问.net webService 简单例子

mjm13 · 2009-11-27 11:19 · java · Java · Apache · WebService · .net · Web · Markdown

原文地址:http://www.iteye.com/topic/151541 在原文基础上加了点注释 呵呵。

所需jar包: saaj.jar wsdl4j-1.5.1.jar commons-discovery-0.2.jar commons-logging-1.0.4.jar jaxrpc.jar axis.jar

这些jar包都在axis项目下, axis下载地址:http://ws.apache.org/axis/

java代码:


// 需导入的类
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;

//              .net webService 地址
		String url="http://localhost:1246/WebSite3/Service.asmx";  
//              .net webService 命名空间
		String namespace = "http://tempuri.org/";  
//              .net webService 需调用的方法
		String methodName = "HelloWorld";  
		String soapActionURI = "http://tempuri.org/HelloWorld";  
		Service service = new Service();
		
		Call call = (Call) service.createCall();

		call.setTargetEndpointAddress(new java.net.URL(url));  
		call.setUseSOAPAction(true);  
//		这个地方没设对就会出现Server was unable to read request的错误  
		call.setSOAPActionURI(soapActionURI);  
//              设置要调用的.net webService方法
		call.setOperationName(new QName(namespace, methodName));  
//              设置该方法的参数,temp为.net webService中的参数名称
		call.addParameter( new QName(namespace,"temp"),  
			    org.apache.axis.encoding.XMLType.XSD_STRING,   
			    javax.xml.rpc.ParameterMode.IN);  
//              设置该方法的返回值
		call.setReturnType(XMLType.XSD_STRING);
//              call.invoke(new Object[] { "kusix" });  中"kusix"为传入参数值
		String ret = (String) call.invoke(new Object[] { "kusix" });  
		System.out.println("返回结果---> " + ret);  

.net 代码


using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

[webservice(namespace = "http://tempuri.org/")]
[webservicebinding(conformsto = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
    public Service () {

        //如果使用设计的组件,请取消注释以下行 
        //InitializeComponent(); 
    }

    [webmethod]
    public string HelloWorld(String temp) {
        return temp+" add by .net";
    }
    
}


评论(4)

  1. #4 楼 · Fly_SkyXin · 2017-08-17 13:25

    AxisFault faultCode: {http://xml.apache.org/axis/}HTTP faultSubcode: faultString: (404)Not Found faultActor: faultNode: faultDetail: {}:return code: 404

    {http://xml.apache.org/axis/}HttpErrorCode:404
    

    报这个错误

  2. #3 楼 · asd51731 · 2012-11-08 11:28

    请问一下,我希望能往.net service端传多个double类型参数,服务器端需要接受三个double类型参数,然后我就复制了 call.addParameter( new QName(namespace,"temp"),
    org.apache.axis.encoding.XMLType.XSD_DOUBLE,
    javax.xml.rpc.ParameterMode.IN);
    改了里面的参数,并且加上了 call.setEncodingStyle( null ); 一直提示 could not find deserializer for type {http://schemas.xmlsoap.org/soap/encoding/}double 您能帮着解决一下吗?

  3. #2 楼 · mjm13 · 2011-05-31 17:18

    这里的temp要对应webService中的参数名称。 看看是不是没对应。。 soap的没弄过。。

  4. #1 楼 · dongyangjava · 2011-05-25 15:25

    问个事情,我在做测试的时候 call.addParameter( new QName(namespace,"temp"),
    org.apache.axis.encoding.XMLType.XSD_STRING,
    javax.xml.rpc.ParameterMode.IN);

    这里指定参数的时候,为什么我把temp任意换为别的字符也不可以呢? 是不是我的soap规则没设置好?

评论功能维护中,当前仅展示历史评论。