当前位置:首页 > C#教程 > C#高级

java 调用C#的webservice


import java.rmi.remoteexception;
import java.util.regex.matcher;
import java.util.regex.pattern;
import javax.xml.rpc.serviceexception;
import org.apache.axis.client.call;
import org.apache.axis.client.service;
public class getdata {
	/**
	 * @param args
	 */
	public static void main(string[] args) {
		// todo auto-generated method stub
		
		  string result = null;
		  try {
			string endpoint = "http://192.168.1.106/service1.asmx?wsdl";
			  //直接引用远程的wsdl文件
			 //以下都是套路 
			  service service = new service();
			  call call = (call) service.createcall();
			  call.settargetendpointaddress(endpoint);
			  call.setoperationname("getlocaljson");//wsdl里面描述的接口名称
			  call.addparameter("name", org.apache.axis.encoding.xmltype.xsd_date,
			                javax.xml.rpc.parametermode.in);//接口的参数
			  call.setreturntype(org.apache.axis.encoding.xmltype.xsd_string);//设置返回类型  
			  
		/*	  java调用net 报服务器未能识别 http 头 soapaction 的值,遇到这种问题时,是因为没有设soapaction 
			  的值,加上这行代码就可以call.setsoapactionuri("http://tempuri.org/getlocaljson"),注意后面的add方法哦,是方法名,一定要带哦*/
			  call.setsoapactionuri("http://tempuri.org/getlocaljson");
			  string temp = "fangchao";
			  result = (string)call.invoke(new object[]{temp});
			  //给方法传递参数,并且调用方法
			  system.out.println("result is "+result);
		} catch (remoteexception e) {
			// todo auto-generated catch block
			e.printstacktrace();
		} catch (serviceexception e) {
			// todo auto-generated catch block
			e.printstacktrace();
		}
	
}
	}


}

结果:result is {"country":"qingdao","leader":"wang","generals":[{"name":"zhangfei","age":12},{"name":"guanyu","age":34}]}

所需的各种jar包:

activation.jar

axis.jar

commons-discovery.jar

commons-logging-1.1.3.jar

jaxrpc.jar

mail.jar

wsdl4j-1.5.1.jar


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐