.net调用java xfire发布的webservice封装soapheader头信息
.net调用java xfire发布的webservice封装soapheader头信息比较麻烦,需要改动到代理类代码。
JAVA服务端验证代码
package chachaba.map.service; import org.codehaus.xfire.MessageContext; import org.codehaus.xfire.handler.AbstractHandler; import org.jdom.Element; 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(); try { // 进行身份验证 ,只有yw88@ccc0c325-fe67-4cad-baab-4ad9ebb4f927的用户为授权用户 if (username.equals("yw88") && password.equals("ccc0c325-fe67-4cad-baab-4ad9ebb4f927")) // 这语句不显示 System.out.println("身份验证通过"); else throw new Exception(); } catch (Exception e) { throw new org.codehaus.xfire.fault.XFireFault("非法的用户名和密码", org.codehaus.xfire.fault.XFireFault.SENDER); } } }
.NET调用流程如下:
1、生成代理类,如StallsService.cs
2、在代理类中添加一个SoapHeader类,完整代码如下:
public class AuthenticationToken : SoapHeader { public string Username; public string Password; }
3、在代理类中添加SoapHeader类参数,
public AuthenticationToken Header = new AuthenticationToken();
4、在方法中添加属性代码
[SoapHeader("Header")] public void deleteInter([System.Xml.Serialization.XmlElementAttribute(IsNullable = true)] string in0) { this.Invoke("deleteInter", new object[] {in0}); }
5、将代理类中的Namespace设置为空,
[System.Web.Services.WebServiceBindingAttribute(Name = "StallsServiceHttpBinding", Namespace = "")]
6、客户端调用代码如下:
StallsService client = new StallsService(); AuthenticationToken myHeader = new AuthenticationToken(); myHeader.Username = "username"; myHeader.Password = "password"; client.Header = myHeader; client.deleteInter("9999");
OK