Creation of a Header that is used in the communication of a WebApi

2

I am creating a program that communicates with a webapi and is giving me this error in creating the header.

I'm using this to create the header:

var tempo = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ");
var httpWebRequest = (HttpWebRequest)WebRequest.Create("teste");
httpWebRequest.Headers.Add("Date"+ tempo);

The output is this:

  

Date2017-09-08T15: 25: 53Z

I wanted it to be this:

  

Date: 2017-09-08T15: 25: 53Z

I have also tested the following code:

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

This code generated the following error:

  

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

    
asked by anonymous 08.09.2017 / 17:55

1 answer

2

The problem is not the string, you need to specify the name of the header:

var tempo = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ");
var httpWebRequest = (HttpWebRequest)WebRequest.Create("teste");
httpWebRequest.Headers.Add("MeuHeader", "Date: " + tempo);

This will work as well:

httpWebRequest.Headers["Meuheader"] = "Date: " + tempo;

I just did not understand if you wanted a header named "Date" or was trying to concatenate the value.

    
08.09.2017 / 18:21