---
title: ".net调用含SoapHeader的xfire服务"
author: "kusix"
date: "2010-12-01T17:58:17Z"
tags: []
categories: ["企业架构"]
slug: "net-soapheader-xfire"
draft: false
lastmod: "2010-12-01T18:01:30Z"
id: 830742
url: "https://kusix.iteye.com/blog/830742"
---

使用webservice做接口时通常要做鉴权，而为了不影响业务逻辑，鉴权信息经常是要放在soapheader中的。

通常java调java比较简单，而这次是.net系统调我们这边的J2EE系统，而我们的服务用的是xfire,这时候就有个问题了，xfire处理soapheader通常是使用inHandlers的拦截方式，对于对外公布的服务WSDL而言，是看不到的。而.net如果引用这个WSDL，生成的客户端自然是不包含头信息的，google之，试了无数种方法，.net客户端发过去的信息都是不包含头信息的。

有3种办法：

**1.使xfire发布出去的WSDL携带有soapheader信息**

google之，无果，如有同学告诉一下方法，这里不胜感激。


**2.修改WSDL**

目前我采取的是这种方法，把xfire生成的WSDL直接改之，使之具有soapheader信息，然后把WSDL发给.net那边的开发人员，直接引入就可以使用。

```java

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://www.xx.com/services/xxService">

<xsd:element name="AuthenticationToken" type="tns:AuthenticationToken" />
      <xsd:complexType name="AuthenticationToken">
        <xsd:sequence>
          <xsd:element minOccurs="0" maxOccurs="1" name="username" type="xsd:string" />
	  <xsd:element minOccurs="0" maxOccurs="1" name="password" type="xsd:string" />
        </xsd:sequence>
        <xsd:anyAttribute />
      </xsd:complexType>
</xsd:schema>

...

<wsdl:message name="AuthSoapHeader">
    <wsdl:part name="AuthenticationToken" element="tns:AuthenticationToken" />
</wsdl:message>

...

<wsdl:operation name="helloworld">
      <wsdlsoap:operation soapAction=""/>
      <wsdl:input name="helloworldRequest">
        <wsdlsoap:body use="literal"/>
	<wsdlsoap:header message="tns:AuthSoapHeader" part="AuthenticationToken" use="literal" />
      </wsdl:input>
      <wsdl:output name="helloworldResponse">
        <wsdlsoap:body use="literal"/>
      </wsdl:output>
</wsdl:operation>

```


**3..net端在调用服务时，使用http方式，拼装soap报文，直接用xml交互**

这个方法肯定是有效的，webservice的原理之一也就是用http传递soap报文，但是太麻烦，实在没办法了再用吧。