How to create a header of type date but in the form of ISO 8601

2

I am creating a communication with a web api and I needed to create a header of type date but formatted to ISO 8601 . What I want is to get the following output:

  

Date: 2017-09-13T08: 21: 08Z

My code is as follows:

var tempo = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ");
var httpWebRequest = (HttpWebRequest)WebRequest.Create("test.com");

What I've already tried:

httpWebRequest.Headers.Add("Date", tempo);
httpWebRequest.Headers.Add($"Date: {DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ")}");
httpWebRequest.Headers["Date"] = tempo;

With these codes I get the following error:

  

System.ArgumentException: 'The' Date 'header must be modified   with the proper property or method. '

    
asked by anonymous 13.09.2017 / 10:36

2 answers

3

The header should be changed as follows:

httpWebRequest.Date = DateTime.UtcNow;

See an example here .

    
13.09.2017 / 11:47
0

I've already found a way to solve this problem so I'll leave my answer to be able to help someone who has the same doubts as me but thanks to those who helped. The answer:

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

The output I get is this:

  

Date: 2017-09-13T08: 21: 08Z

    
13.09.2017 / 11:51