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

Asked

Viewed 810 times

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 :

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

I wish it was 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: 'Date header has to be modified with the appropriate property or method.'

1 answer

2


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

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

That way it’ll work too:

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

I just didn’t understand if you wanted a header named "Date" or were trying to concatenate the value.

  • How do I only show the Date: + time

  • Without appearing the brings the Meuheader

  • As far as I know the header must have a name, just be careful with the name you give, there are some patterns to follow otherwise the firewall may end up ignoring it.

  • I wanted my header to look like this for example : 'Date: 2016-06-29T12:00:00Z'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.