---
title: "java通过axis访问.net webService 简单例子"
author: "mjm13"
date: "2009-11-27T11:19:37Z"
tags: [".net", "Apache", "Java", "Web", "WebService"]
slug: "java-axis-net-webservice"
draft: false
lastmod: "2009-12-03T15:10:04Z"
id: 531016
url: "https://mjm13.iteye.com/blog/531016"
---

原文地址：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代码：

```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 代码

```c#

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";
    }
    
}


```