本文介绍了从一个的OperationContext SOAP头中获取的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 C#下面的代码看起来为 apiKey 在以下 SOAP 标题:



的SOAP Header:

 <肥皂:页眉和GT; 
<身份验证和GT;
< apiKey> CCE4FB48-865D-4DCF-A091-6D4511F03B87< / apiKey>
< /验证>
< / SOAP:包头>



C#:



这是我迄今为止:

 公共字符串GetAPIKey(的OperationContext的OperationContext)
{
字符串apiKey = NULL;

//只看传入邮件头。
的for(int i = 0; I< OperationContext.Current.IncomingMessageHeaders.Count;我++)
{
MessageHeaderInfo H = OperationContext.Current.IncomingMessageHeaders [I]

//用正确的名称的任何引用参数。
如果(h.Name ==apiKey)
{
//读取这个头的值。
的XmlReader XR = OperationContext.Current.IncomingMessageHeaders.GetReaderAtHeader(ⅰ);
apiKey = xr.ReadElementContentAsString();
}
}

//返回API密钥(如果存在的话,返回null如果不)。
返回apiKey;
}



问题:返回而不是实际的 apiKey 值:

  CCE4FB48-865D-4DCF-A091-6D4511F03B87 

更新1:



我加了一些记录。它看起来像 h.Name 其实是在身份验证,这意味着它实际上不会寻找apiKey,然后意味着它将无法。检索值



有没有办法抓住< <$ C里面; apiKey /> $ C><认证/>



更新2:



结束了使用下面的代码:

 如果(h.Name ==认证)
{
//读取这个头的值。
的XmlReader XR = OperationContext.Current.IncomingMessageHeaders.GetReaderAtHeader(ⅰ);
xr.ReadToDescendant(apiKey);
apiKey = xr.ReadElementContentAsString();
}


解决方案

我觉得你的 h.Name 验证,因为它是根型和apiKey是认证的财产键入。尝试 h.Name 的记录值,一些日志文件,并检查什么呢返回。

 如果(h.Name ==认证)
{
//读取值这个头。
的XmlReader XR = OperationContext.Current.IncomingMessageHeaders.GetReaderAtHeader(ⅰ);
// apiKey = xr.ReadElementContentAsString();
xr.ReadToFollowing(验证);
apiKey = xr.ReadElementContentAsString();
}


I have the following code in C# that looks for an apiKey in the the following SOAP header:

SOAP Header:

<soap:Header>
   <Authentication>
       <apiKey>CCE4FB48-865D-4DCF-A091-6D4511F03B87</apiKey>
   </Authentication>
</soap:Header>

C#:

This is what I have so far:

public string GetAPIKey(OperationContext operationContext)
{
    string apiKey = null;

    // Look at headers on incoming message.
    for (int i = 0; i < OperationContext.Current.IncomingMessageHeaders.Count; i++)
    {
        MessageHeaderInfo h = OperationContext.Current.IncomingMessageHeaders[i];

        // For any reference parameters with the correct name.
        if (h.Name == "apiKey")
        {
            // Read the value of that header.
            XmlReader xr = OperationContext.Current.IncomingMessageHeaders.GetReaderAtHeader(i);
            apiKey = xr.ReadElementContentAsString();
        }
    }

    // Return the API key (if present, null if not).
    return apiKey;
}

PROBLEM: Returning null instead of the actual apiKey value:

CCE4FB48-865D-4DCF-A091-6D4511F03B87

UPDATE 1:

I added some logging. It looks like h.Name is in fact "Authentication", which means it won't actually be looking for "apiKey", which then means it won't be able to retrieve the value.

Is there a way to grab the <apiKey /> inside of <Authentication />?

UPDATE 2:

Ended up using the following code:

if (h.Name == "Authentication")
{
  // Read the value of that header.
  XmlReader xr = OperationContext.Current.IncomingMessageHeaders.GetReaderAtHeader(i);
  xr.ReadToDescendant("apiKey");
  apiKey = xr.ReadElementContentAsString();
}
解决方案

I think your h.Name is Authentication because it is root type and apiKey is property of Authentication type. Try logging values of h.Name to some log file and check what does it return.

if (h.Name == "Authentication")
{
    // Read the value of that header.
    XmlReader xr = OperationContext.Current.IncomingMessageHeaders.GetReaderAtHeader(i);
    //apiKey = xr.ReadElementContentAsString();
    xr.ReadToFollowing("Authentication");
    apiKey = xr.ReadElementContentAsString();
}

这篇关于从一个的OperationContext SOAP头中获取的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 11:28