简体   繁体   English

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

[英]WCF Behavior Extension to BizTalk WCF-WebHttp Send Port not populating Date Http Header

I have a BizTalk 2013 app (not R2) that needs to send a Json document to an external vendor's RESTful api. 我有一个BizTalk 2013应用程序(不是R2),需要将Json文档发送到外部供应商的RESTful API。 The vendor requires three Http headers: 供应商需要三个Http标头:

  1. Content-Type: application/json 内容类型:application / json

  2. Date: in ISO8601 UTC format 日期: 采用ISO8601 UTC格式

  3. Authorization: custom auth. 授权: 自定义身份验证。 using a constructed string that includes the above Date value run through the HMACSHA1 hash 使用包含上述日期值的构造字符串,该字符串贯穿HMACSHA1哈希

In BizTalk, my outbound (xml) message goes to the Send Port, there is a Custom Pipeline Component that transforms to Json using JSON.Net. 在BizTalk中,我的出站(xml)消息转到“发送端口”,有一个自定义管道组件,该组件使用JSON.Net转换为Json。 So far, so good. 到现在为止还挺好。 To add the headers which are unique per message, I created a WCF Behavior extension that implements IClientInspector and IEndpointBehavior. 为了添加每个消息唯一的标头,我创建了一个WCF Behavior扩展,该扩展实现了IClientInspector和IEndpointBehavior。 In BeforeSendRequest(), I get a reference to the HttpRequestMessageProperty for the request. 在BeforeSendRequest()中,我获得了对该请求的HttpRequestMessageProperty的引用。

I can successfully add to the Headers Collection a ContentType header and an Authorization header. 我可以成功地将ContentType标头和Authorization标头添加到Headers Collection。 I cannot add a Date header - no errors, just no header value when examining with Fiddler. 我无法添加Date标头-没有错误,使用Fiddler检查时没有标头值。

I read somewhere that Date is a restricted header and for a workaround I could use Reflection to get around it. 我在某处读到Date是受限制的标头,并且要解决此问题,可以使用Reflection来解决它。 Eg 例如

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

That didn't work either. 那也不起作用。 I'm really stumped with: 1. Why no Date header at all is on my request? 我真的很困惑:1.为什么我的请求中根本没有Date标头? 2. If there was one, how I could manipulate it as I need to give that is "restricted"? 2.如果有一个,我该如何操作,因为我需要给它“限制”?

I tried two different options: a WCF behavior extension: 我尝试了两个不同的选项: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);

and a Custom Pipeline Component in a Send Pipeline 和发送管道中的自定义管道组件

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);

in either case, I can set the Content-Type, the Authorization and test date - tweDate just to test, but I can not set the actual Date header. 无论哪种情况,我都可以设置Content-Type,授权和测试日期-tweDate仅用于测试,但不能设置实际的Date标头。

Yes indeed Date is a special HTTP Header that respresents the date and time at which the message originated but that doesn't prevent you from using it. 是的,确实,Date是一个特殊的HTTP标头,它代表消息生成的日期和时间,但这并不妨碍您使用它。 BUT the date has to be in RFC 822 Format Tue, 15 Nov 1994 08:12:31 GMT 但是日期必须采用RFC 822格式,星期二,1994年11月15日08:12:31 GMT
Another thing why do you make it the hard way by coding a behaviour while you could just use HTTPHeaders to add your custom header in your custom pipeline component plus this is so much cleaner 另一件事,为什么要编码行为,而只使用HTTPHeaders在自定义管道组件中添加自定义标头,这又使之变得困难呢?

So try to convert your date in the correct format or use 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 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM