WCF行为扩展到BizTalk WCF-WebHttp发送端口不填充日期Http标头

时间:2015-07-09 17:18:01

标签: json wcf http-headers biztalk wcf-behaviour

我有一个BizTalk 2013应用程序(不是R2)需要将Json文档发送到外部供应商的RESTful api。供应商需要三个Http标头:

  1.   

    Content-Type:application / json

  2.   

    日期:采用ISO8601 UTC格式

  3.   

    授权:自定义身份验证。使用包含上述Date值的构造字符串,运行HMACSHA1哈希

  4. 在BizTalk中,我的出站(xml)消息转到发送端口,有一个自定义管道组件使用JSON.Net转换为Json。到现在为止还挺好。为了添加每个消息唯一的标头,我创建了一个WCF行为扩展,它实现了IClientInspector和IEndpointBehavior。在BeforeSendRequest()中,我获得了对请求的HttpRequestMessageProperty的引用。

    我可以成功地向Header Collection添加ContentType标头和Authorization标头。我无法添加Date标头 - 没有错误,只有在使用Fiddler进行检查时没有标头值。

    我在某处看到Date是一个受限制的标题,对于解决方法,我可以使用Reflection来解决它。 E.g。

        MethodInfo priMethod = headers.GetType().GetMethod("AddWithoutValidate", BindingFlags.Instance | BindingFlags.NonPublic);
        priMethod.Invoke(headers, new[] { "Date", ISODateTimestamp });
    

    这也不起作用。我真的很难过:1。为什么我的请求根本没有Date标题? 2.如果有的话,我怎么能操纵它,因为我需要给它“限制”?

    我尝试了两种不同的选项:WCF行为扩展名:

    public object BeforeSendRequest(ref Message request, System.ServiceModel.IClientChannel channel)
      {
          System.Diagnostics.Debug.Print("Entering BeforeSendRequest()");
    
          try
          {
              HttpRequestMessageProperty httpRequest = null;
    
              if (request.Properties.ContainsKey(HttpRequestMessageProperty.Name))
              {
                  httpRequest = request.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
              }
    
              WebHeaderCollection headers = httpRequest.Headers;
    
              headers.Add(HttpRequestHeader.ContentType, "application/json");
              headers.Add(string.Format("{0}:{1}", "Date", _taxwareHelper.ISODateTimestamp));
              headers.Add("tweDate", _taxwareHelper.ISODateTimestamp);
              headers.Add(HttpRequestHeader.Authorization, _taxwareHelper.AuthorizationString);
    

    和发送管道中的自定义管道组件

    string httpHeaderValue =
        new StringBuilder()
        .Append("Content-Type: application/json")
        .Append("\n")
        //.Append(string.Format("Date:{0}", taxwareHelper.ISODateTimestamp))
        //.Append("\n")
        .Append(string.Format("Date:{0}", "Fri, 10 Jul 2015 08:12:31 GMT"))
        .Append("\n")
        .Append(string.Format("tweDate:{0}", taxwareHelper.ISODateTimestamp))
        .Append("\n")
        .Append(string.Format("Authorization:{0}", taxwareHelper.AuthorizationString))
        .ToString();
    
    pInMsg.Context.Write("HttpHeaders", "http://schemas.microsoft.com/BizTalk/2006/01/Adapters/WCF-properties", httpHeaderValue);
    

    在任何一种情况下,我都可以设置Content-Type,Authorization和测试日期 - tweDate只是为了测试,但我无法设置实际的Date标题。

1 个答案:

答案 0 :(得分:0)

是的确实日期是一个特殊的HTTP标头,它表示消息来源的日期和时间,但不会阻止您使用它。但日期必须是RFC 822 Format Tue,1994年11月15日08:12:31 GMT
另外一个原因是,为什么你可以通过编码行为来实现这一点,而你可以使用HTTPHeaders在自定义管道组件中添加自定义标头,这样就更加清晰了

因此,请尝试以正确的格式转换日期或使用HTTPHeaders

http://www.codit.eu/blog/2013/04/30/using-httpheaders-with-wcf-webhttp-adapter-on-biztalk-2013/ http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

相关问题