如何为C#Web服务客户端添加自定义SOAP标头调用Axis 1.4 Web服务

时间:2011-08-17 06:33:04

标签: c# java web-services axis webservice-client

我正在尝试编写一个C#应用程序,它是一个调用Java Web服务的Webservice客户端(使用Axis 1.4),但是我无法让它工作。 Java WS需要自定义SOAP标头进行身份验证,因此我在C#应用程序中添加了一个。 VS 2010中生成的代码不包含SOAP标头,因此我在项目中添加了MySOAPHeader类:

namespace AddProvinceCityDemo
{
 [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://service.itthrm.vnitt.org")]
    [System.Xml.Serialization.XmlRootAttribute("securitytoken", Namespace = "http://service.itthrm.vnitt.org", IsNullable = false)]
    public class MySOAPHeader:SoapHeader
    {
        public String username;
        public String password;
    }
}

我还通过添加MySOAPHeader变量

来更改Reference.cs中生成的代码
public partial class ProvinceServiceClient : System.ServiceModel.ClientBase<AddProvinceCityDemo.ProvinceService.ProvinceService>, AddProvinceCityDemo.ProvinceService.ProvinceService {

        public MySOAPHeader customSOAPHeader;
        public ProvinceServiceClient() {
        }
         //.........................
}

最后,我使用以下代码调用WS:

MySOAPHeader authentication = new MySOAPHeader();
 authentication.username = username;

  authentication.password = encryptedpass;
 //end set SOAP header information

 ProvinceServiceClient provinceservice = new ProvinceServiceClient();

 provinceservice.customSOAPHeader = authentication;
 Province[] arrprovince = null;

 arrprovince = provinceservice.findAll();

但它最终会抛出一个错误:"Failed to retrieve the SOAP header"

编写Java版本的代码很容易:

SOAPHeaderElement oHeaderElement;

oHeaderElement = new SOAPHeaderElement(namespace, "securityHeader");
oHeaderElement.setPrefix"sec");
oHeaderElement.setMustUnderstand(false);
oElement = oHeaderElement.addChildElement("username");
oElement.addTextNode(username);
oElement = oHeaderElement.addChildElement("password");
oElement.addTextNode(passwordEncrypt); 

我如何在.NET和C#中做同样的事情?

0 个答案:

没有答案