`

C#与Axis2互操作的问题(SoapHeader)

 
阅读更多
 

C# 调用WebService 时 soapheader验证问题!

问题:C#调用webservice时需要 soapheader中传送对方指定的信息,.net 平台自动生成的类中没有相关的接口

实现方法:
1.C#调用java 编写的webservice时不会自动生成 soapheader 类接口的,需要改动Reference.cs。
2.改动方法
   a、 继承System.Web.Services.Protocols.SoapHeader ,生成一个新类(MyHeader,这里名字无所谓),类中扩展调要webservice 试soapheader中要求的成员,名称一定要与对方要求的一样

    public class MyHeader : System.Web.Services.Protocols.SoapHeader
    {
        public string user; //= "12650000238BF747AD";
        public string pwd ;//= "70947EFA9D77E413502C24B0DD252F66";
        public MyHeader()
        {
            user = "";
            pwd = ""; 
        }
    }

  需要注意的是: user,pwd是对方要求的,名称一定要一致。

  b、代理类中增加 成员

          public MyHeader Header;

  其中 MyHeader  的实例 Header 必须与对方要求的格式一样,这个是血的教训,不能像其他对象一样随便起名字。原格式

       <soapenv:Header>
            <ns:getPersonInfo xmlns:ns="http://service.wondersgroup.com">
            <ns:user>12650000238BF747AD</ns:user>
            <ns:pwd>70947EFA9D77E413502C24B0DD252F66</ns:pwd>
            </ns:getPersonInfo>
       </soapenv:Header>

  c、Header对象的 user,pwd 成员赋值。

  d、与webservice 接口函数绑定。

       [System.Web.Services.Protocols.SoapHeader("Header")]

经过不断的测试,才发现这些,给我忽然提示的是我在对象浏览器中查看public sealed class SoapHeaderAttribute类时里面有成员MemberName,看说明,它不是节点名称,而是节点值,那节点名称怎么来的呢? 忽然就想起来,节点的名称直接取对象的名称。所以以上红色部分一定要注意。

 

以上程序 vs2008上调试成功!

 

java写好的webservice服务部分代码:
public class AuthenticationHandler extends AbstractHandler {


public void invoke(MessageContext cfx) throws Exception {
if (cfx.getInMessage().getHeader() == null) {
throw new org.codehaus.xfire.fault.XFireFault("请求必须包含验证信息", org.codehaus.xfire.fault.XFireFault.SENDER);
}
Element token = cfx.getInMessage().getHeader().getChild("AuthenticationToken");//在客户端需要与此验证一致命名
if (token == null) {
throw new org.codehaus.xfire.fault.XFireFault("请求必须包含身份验证信息", org.codehaus.xfire.fault.XFireFault.SENDER);
}

String username = token.getChild("Username").getValue();//在客户端需要与此验证一致命名
String password = token.getChild("Password").getValue();//在客户端需要与此验证一致命名
System.out.println("username ="+username);
System.out.println("Password ="+password);
try {
// 进行身份验证 ,只有xf@123456的用户为授权用户
if (username.equals("cnpciwap") && password.equals("cnpciwap"))
// 这语句不显示
System.out.println("身份验证通过");
else
throw new Exception();
} catch (Exception e) {
throw new org.codehaus.xfire.fault.XFireFault("非法的用户名和密码", org.codehaus.xfire.fault.XFireFault.SENDER);
}
}
}


客户端操作:
1.添加服务引用,在自动生成的代理类Reference.cs中添加头信息类:
public class AuthenticationToken : SoapHeader {
public string Username;
    public string Password;
}
2.在服务类Assessment声明

public AuthenticationToken Header = new AuthenticationToken();
3.在接口方法send()上面添加 [SoapHeader("Header")];
4.在客户端给属性赋值并调用方法:
Assessment cs = new Assessment();
cs.Header.Username ="ueser";
cs.Header.Password="user";
cs.send("1","2",2,"4");
在运行到调用方法时候,总是弹出“请求必须包含身份验证信息”的异常。
求帮助。

 

step1: 首先,选择当前.NET项目,右键|添加web引用,弹出图1:



 

图1

         在URL地址栏中输入要引用的webservice所在的目录,点“前往”后可以看到所有的webservice列表,然后选择你要调用的那个具体的 webservice,假定我们要调用ContactService,则点击“ContactService [wsdl]”即可。当然你也可以在URL地址栏中直接输入具体的webservice地址,直接定位到你要调用的webservice,见图2



 

         图2

         在“Web引用名”的文本框中输入引入的webservice的名字(随便起),然后点击“添加引用”。至此,webservice就已经导入到。Net 项目中了,可以看到,。Net项目多出了“Web References”文件夹,并且其中列出了你刚才引入的webservice实例,即localhost(名字随便起)。如果我们配置了调用 webservice口令的话,实际上我们刚刚引入的localhost webservice实例是不能正常使用的,还需做一些其他配置。



 图3

step2: 在.NET项目的根目录下新建一个C#类文件,名字叫做AuthenticationToken.cs,其中的代码如下:

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.Services.Protocols;

using System.Web.Services;

 

namespace WebService

{

    [System.Serializable]

    [System.Xml.Serialization.XmlType(Namespace = "OKService")]

    [System.Xml.Serialization.XmlRoot(Namespace = "OKService", IsNullable = false)]

    public class AuthenticationToken : SoapHeader  

    {

        public string username = "1234548"; //用户名

        public string password = "dfdfdsf"; //口令

    }

}

 

Step3:新建一个Default.aspx,在服务端的Default.aspx.cs文件中调用webservice,实例代码如下:

localhost.ContactService ws = new TestProj.localhost.ContactService();

TestProj.localhost.ContactBean cb = new TestProj.localhost.ContactBean();

cb.username = "pizza_008";

cb.realname = "张小律测试";

cb.tel = "31650633";

cb.mobile = "15151551511";

cb.groupId = "123";

ws.addContact(cb);

 

Step4:可以看到,.NET会提示错误,这是因为我们还没有对修改相应的代理类。鼠标移动到ContactService处,右键|转到定义, 进入Reference.cs中(代理类),找到webservice接口名称(本例即ContactService),在public ContactService() { //….. }的上方添加以下代码:

public AuthenticationToken SoapHeader = new AuthenticationToken();  //该行代码需手动添加

 

Step5:在Reference.cs代理类中,找到该接口的方法名,例如,我们刚刚发布webservice实例(localhost)中包括以下方法:

/// <remarks/>

        public event getContactsCompletedEventHandler getContactsCompleted;

       

        /// <remarks/>

        public event addContactCompletedEventHandler addContactCompleted;

       

        /// <remarks/>

        public event modifyContactCompletedEventHandler modifyContactCompleted;

       

        /// <remarks/>

        public event removeContactCompletedEventHandler removeContactCompleted;

       

        /// <remarks/>

        public event isContactExistsCompletedEventHandler isContactExistsCompleted;

ctrl+F搜索每个已发布的方法,如addContact方法,找到该方法后,在方法头上方添加以下代码:

[SoapHeader("SoapHeader")] //该行代码需手动添加

public object addContact([System.Xml.Serialization.XmlElementAttribute(IsNullable=true)] ContactBean in0) {

object[] results = this.Invoke("addContact", new object[] { in0});

return ((object)(results[0]));

}

 

Step6:按照Step5的操作依次找到所有webservice接口方法,并给所有这些方法头添加以下代码:

[SoapHeader("SoapHeader")] //该行代码需手动添加

 

Step7:经过以上6个步骤,webservice已经可以正常使用了。

 

  • 大小: 27.8 KB
  • 大小: 32.3 KB
  • 大小: 8.5 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics