【问题标题】:BizTalk WCF-WebHTTP REST Client with Body and Header Sign with static portBizTalk WCF-WebHTTP REST 客户端与正文和标题签名与静态端口
【发布时间】:2021-01-11 15:07:09
【问题描述】:

我需要将 REST 客户端编写为需要自定义标头和 JSON 正文的服务(我们称之为 REST SERVICE)。

其中两个标头必须具有根据标头(也是动态的)和整个 JSON 正文中的其他值计算的签名。

我还托管了一个 Web 服务,用于为我的客户提供服务。在我的服务正文中,我需要将一些值传递给 REST SERVICE 的标头。

我在编排的消息分配中进行了动态 WCF-WebHTTP 端口和整个标头计算。它有效,但我想创建静态端口。 如何实现body和header检查——如何计算请求body的签名并将结果传递给带有静态WCF-WebHTTP端口的同一请求的header?

【问题讨论】:

  • 如果需要添加自定义header,可以实现IDispatchMessageInspector接口或者IClientMessageInspector接口。 IDispatchMessageInspector 接口需要在服务端实现,IClientMessageInspector 是在客户端实现。有关 IDispatchMessageInspector 的更多信息,请参考此链接:docs.microsoft.com/en-us/dotnet/api/…
  • @DingPeng 我无法阅读 http 正文。我得到 一些散列文本 xml,而不是格式良好的。
  • 你没有通过代理类调用服务吗?
  • 我不明白你的问题。我想在我的自定义行为类中检查 BeforeSendRequest 方法中的消息请求对象,就像在这个示例代码中一样:stackoverflow.com/questions/35631372/…
  • 您不知道如何将 BeforeSendRequest 方法应用于您的服务吗?

标签: rest wcf biztalk


【解决方案1】:

我终于能够通过我的自定义行为来完成此任务。

诀窍是解码二进制形式的 body,因为它原来是 base64 编码

这里是我如何操作身体的示例代码:

private Message TransformMessage2(Message oldMessage) { 

Message newMessage = null; 

MessageBuffer msgbuf = oldMessage.CreateBufferedCopy(int.MaxValue);

Message tmpMessage = msgbuf.CreateMessage();

XmlDictionaryReader xdr = tmpMessage.GetReaderAtBodyContents

XmlDocument xdoc = new XmlDocument(); 

xdoc.Load(xdr); xdr.Close(); 

byte[] bodyByte = Convert.FromBase64String(xdoc.InnerXml.ToString().Replace("<Binary>", "").Replace("</Binary>", ""));

String bodyString = Encoding.UTF8.GetString(bodyByte, 0, bodyByte.Length); 

bodyString = bodyString.Replace("1234321", "9999999"); 

bodyByte = Encoding.UTF8.GetBytes(bodyString);

bodyString = "<Binary>" + Convert.ToBase64String(bodyByte) + "</Binary>"; 

xdoc = new XmlDocument(); 

xdoc.LoadXml(bodyString); 

MemoryStream ms = new MemoryStream(); 

XmlWriter xw = XmlWriter.Create(ms);

xdoc.Save(xw); xw.Flush(); xw.Close();  ms.Position = 0; 

XmlReader xr = XmlReader.Create(ms);

newMessage = Message.CreateMessage(oldMessage.Version, null, xr);   newMessage.Headers.CopyHeadersFrom(oldMessage); 

newMessage.Properties.CopyProperties(oldMessage.Properties); 

return newMessage;  }

【讨论】:

    猜你喜欢
    • 2019-06-23
    • 1970-01-01
    • 2012-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-23
    相关资源
    最近更新 更多