如何添加“平面"弹性网络服务调用的消息头? [英] How to add a "flat" message header to a flex web service call?

查看:17
本文介绍了如何添加“平面"弹性网络服务调用的消息头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 flex 客户端连接到需要添加身份验证令牌作为标题的 Web 服务,名为身份".预期消息的一个示例是:

I have a flex client connecting to a web service that needs an authentication token added as a header, named "Identity". An example of the expected message is:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <Identity xmlns="ns">2188dcbe-0325-4c1e-9a77-19110e0ea99f</Identity>
    <To s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">https://localhost:8001/MyService</To>
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">ns/MyService/MyOperation</Action>
  </s:Header>
  <s:Body>
    ...
  </s:Body>
</s:Envelope>

如何在 flex 中添加这种标题?我已经尝试使用 .addHeader() 和 .addSimpleHeader,但是这两个似乎都向标题元素添加了子元素,例如:

How do I add this kind of header in flex? I've tried using both .addHeader() and .addSimpleHeader, but both of these seem to add sub-elements to the header element like:

<Identity xmlns="ns">
  <Value>34234234-234-234-23-234324</Value>
</Identity>

我知道这应该是可能的,因为 flex 正在为To"和Action"标头执行此操作.addHeader 似乎建议您可以提供原始 XML,但我无法使其正常工作.

I know this should be possible, since flex is doing this for the "To" and "Action" headers. The documentation for addHeader seems to suggest you can give it raw XML but I haven't been able to make it work.

提前感谢您的指点!

推荐答案

有几种方法可以做到.我个人更喜欢覆盖本机 SOAPEncoder 类,它使您可以在发送之前访问实际的肥皂信封.这使您能够拥有更多控制权并添加诸如 ws-addressing 和自定义身份验证标头之类的内容.

There's a couple of ways of doing it. I personally prefer overriding the native SOAPEncoder class which given you access to the actual soap envelope before it is sent. This enables you to have more control and add things like ws-addressing and custom authenication headers.

public class myEncoder 
extends SOAPEncoder
{

private const WSSE_NS:Namespace = new Namespace("wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wsswssecurity-secext-1.0.xsd");

    //--------------------------------------------------------------------------
    //
    // Constructor
    // 
    //--------------------------------------------------------------------------
    public function wsseEncoder()
    {
        super();
    }

    //--------------------------------------------------------------------------
    //
    // Methods
    // 
    //--------------------------------------------------------------------------

    /**
     * <p>  override super classes method and recieve raw soap message to manpulate soap envelope
     * </p>
     */
    public override function encodeRequest(args:* = null, headers:Array = null):XML
    {
        //get soap envelope from super class
        var SOAPEnvelope:XML = super.encodeRequest(args, headers);

               //create a header in xml and append it at as a child
        securityHeaderXml = <Security/>;
        securityHeaderXml.setNamespace(WSSE_NS);
        securityHeaderXml.@[mustUnderstand] = 1;

        //set deafult ws-security namespace - filters to all child nodes
        securityHeaderXml.setNamespace(WSSE_NS);

            var id:XML = <Identity/>;
            id.appendChild('value here');
            SOAPEnvelope.prependChild(id);

            SOAPEnvelope.prependChild(headerXml);

        return SOAPEnvelope;
    }


    }
}

然后您需要做的就是将默认编码器更改为此编码器,如果您使用生成 Web 服务类,请转到 serviceBase 并查找方法Call"并将此行更改为此

Then all you need to do is change the default encoder to this one, if your using generate web service classes go to the serviceBase and look for the method 'Call' and change this line to this

var enc:SOAPEncoder = new myEncoder();//而不是这个 -> new SOAPEncoder();

var enc:SOAPEncoder = new myEncoder(); //instead of this -> new SOAPEncoder();

如果不是类似 myService.encoder = new myEncoder();

if not iots something like myService.encoder = new myEncoder();

就这么简单.

显然覆盖编码器类给你更多的控制.您还可以对 SOAPDecoder 类执行相同的操作,以在反序列化之前捕获 soap 信封.

Obviously override the encoder class gives you alot more control. You can also do the same with the SOAPDecoder class to catch the soap envelope before it get de-serialised.

希望能帮到你

乔恩

这篇关于如何添加“平面"弹性网络服务调用的消息头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆