简体   繁体   中英

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. The vendor requires three Http headers:

  1. Content-Type: application/json

  2. Date: in ISO8601 UTC format

  3. Authorization: custom auth. using a constructed string that includes the above Date value run through the HMACSHA1 hash

In BizTalk, my outbound (xml) message goes to the Send Port, there is a Custom Pipeline Component that transforms to Json using JSON.Net. So far, so good. To add the headers which are unique per message, I created a WCF Behavior extension that implements IClientInspector and IEndpointBehavior. In BeforeSendRequest(), I get a reference to the HttpRequestMessageProperty for the request.

I can successfully add to the Headers Collection a ContentType header and an Authorization header. I cannot add a Date header - no errors, just no header value when examining with Fiddler.

I read somewhere that Date is a restricted header and for a workaround I could use Reflection to get around it. 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? 2. If there was one, how I could manipulate it as I need to give that is "restricted"?

I tried two different options: a WCF behavior extension:

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.

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. BUT the date has to be in RFC 822 Format Tue, 15 Nov 1994 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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