设为首页收藏本站
开启辅助访问
切换到宽版

创星网络[分享知识 传递快乐]

 找回密码
 立即注册

QQ登录

只需一步,快速开始

用新浪微博登录

只需一步,快速搞定

搜索
查看: 3732|回复: 0
打印 上一主题 下一主题

java调用.net的 WebService

[复制链接]

我玩的应用:

跳转到指定楼层
楼主
发表于 2012-12-13 19:57:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1
在vs2005中新建WebService,
在App_Code
建立新类:Class1.cs
-----
  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Web;  
  5. using System.Web.Security;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8. using System.Web.UI.WebControls.WebParts;  
  9. using System.Web.UI.HtmlControls;  
  10.   
  11. /// <summary>  
  12. /// Class1 的摘要说明  
  13. /// </summary>  
  14. public class Class1  
  15. {  
  16.     public Class1()  
  17.     {  
  18.         //  
  19.         // TODO: 在此处添加构造函数逻辑  
  20.          
  21.     }  
  22.   
  23.     //  
  24.     public String join(String s1, String s2)  
  25.     {  
  26.         String s = "*****" + s1 + s2 + "&&&&&&";  
  27.         return s;  
  28.     }  
  29. }  
复制代码
修改:Service.cs
  1. using System;  
  2. using System.Web;  
  3. using System.Web.Services;  
  4. using System.Web.Services.Protocols;  
  5.   
  6. [WebService(Namespace = "http://tempuri.org/")]  
  7. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  8. public class Service : System.Web.Services.WebService  
  9. {  
  10.     public Service () {  
  11.   
  12.         //如果使用设计的组件,请取消注释以下行   
  13.         //InitializeComponent();   
  14.     }  
  15.   
  16.     [WebMethod]  
  17.     public string HelloWorld(String s) {  
  18.         Class1 c1 = new Class1();  
  19.         String sp = c1.join("我是中国",s);  
  20.         return (sp+"Hello World,This WebService powered by luinstein");  
  21.     }  
  22.       
  23. }  
复制代码
发布WebService

2

在Myeclipse里新建一个java项目
要使用到axis1.4

使用测试类testwebservice.java:
  1. package test;  
  2.   
  3. import java.net.MalformedURLException;  
  4. import java.rmi.RemoteException;  
  5.   
  6. import javax.xml.namespace.QName;  
  7. import javax.xml.rpc.ParameterMode;  
  8. import javax.xml.rpc.ServiceException;  
  9. import javax.xml.rpc.encoding.XMLType;  
  10.   
  11. import org.apache.axis.client.Call;  
  12. import org.apache.axis.client.Service;  
  13.   
  14.   
  15.   
  16.   
  17.   
  18. public class testwebservice {  
  19.   
  20.     /**
  21.      * @param args
  22.      * @throws RemoteException  
  23.      * @throws ServiceException  
  24.      * @throws MalformedURLException  
  25.      */  
  26.     public static void main(String[] args) throws RemoteException, ServiceException, MalformedURLException {  
  27.          
  28.   
  29.         String url="http://localhost:2019/WebServiveTest/Service.asmx";     
  30.         String namespace = "http://tempuri.org/";     
  31.         String methodName = "HelloWorld";     
  32.         String soapActionURI = "http://tempuri.org/HelloWorld";     
  33.         Service service = new Service();     
  34.         Call call = (Call) service.createCall();     
  35.         call.setTargetEndpointAddress(new java.net.URL(url));     
  36.         call.setUseSOAPAction(true);     
  37.         //这个地方没设对就会出现Server was unable to read request的错误     
  38.         call.setSOAPActionURI(soapActionURI);     
  39.         call.setOperationName(new QName(namespace, methodName));     
  40.         /*这里如果设置成call.addParameter(new QName(namespace,"s"), XMLType.XSD_STRING,   
  41.           ParameterMode.IN);就是调用document风格的.net服务端   
  42.           如果设反了,.net服务端就接不到参数,接到的是null   
  43.         */   
  44.         //call.addParameter("s", XMLType.XSD_STRING, ParameterMode.IN);     
  45.         call.addParameter(new QName(namespace,"s"), XMLType.XSD_STRING, ParameterMode.IN);     
  46.         call.setReturnType(XMLType.XSD_STRING);     
  47.         String ret = (String) call.invoke(new Object[] { "kusix" });     
  48.         System.out.println("返回结果---> " + ret);   
  49.   
  50.   
  51.     }  
  52.          
  53.          
  54.          
  55.   
  56. }  
复制代码
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|创星网络 ( 苏ICP备11027519号|网站地图  

GMT+8, 2024-9-22 07:40 , Processed in 0.076142 second(s), 24 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表