HTTPWebRequest Date Header

  • Thread starter Thread starter Mike Koerner
  • Start date Start date
M

Mike Koerner

Hi,

I am having problems setting the HttpWebRequest Date header. I understand
that it is a restricted header and I do receive the
"This header must be modified with the appropriate property." Is there a
way to make sure that the date header is sent over or a way to work around
this exception?

Here's a sample of the code:

System.Net.HttpWebRequest wr;
wr = (HttpWebRequest)WebRequest.Create(requestUrl);
System.Net.WebHeaderCollection whcHeaders = wr.Headers;
whcHeaders.Add(@"Date" , DateTime.Now.ToString("r"));

Thanks,
Mike
 
Hi Mike,

We have reviewed this issue and are currently researching on it. We will
update you ASAP. Thanks for your patience!

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Hi Mike,

Welcome to ASPNET newsgroup.
As for the "Date" http header you mentioned, based on the HttpWebRequest's
Documentation, it is not included in the headers collection , also there is
no property in the HttpWebRequest class for us to manually adjust it since
it is automatically assigned to the current Date by runtime.

#HttpWebRequest.Headers Property
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemnethttpwebrequestclassheaderstopic.asp

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)





--------------------
| From: "Mike Koerner" <[email protected]>
| Subject: HTTPWebRequest Date Header
| Date: Fri, 16 Sep 2005 15:58:48 -0700
| Lines: 19
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.3790.1830
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.1830
| X-RFC2646: Format=Flowed; Original
| Message-ID: <#[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: 12.175.2.2
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:125237
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Hi,
|
| I am having problems setting the HttpWebRequest Date header. I
understand
| that it is a restricted header and I do receive the
| "This header must be modified with the appropriate property." Is there a
| way to make sure that the date header is sent over or a way to work
around
| this exception?
|
| Here's a sample of the code:
|
| System.Net.HttpWebRequest wr;
| wr = (HttpWebRequest)WebRequest.Create(requestUrl);
| System.Net.WebHeaderCollection whcHeaders = wr.Headers;
| whcHeaders.Add(@"Date" , DateTime.Now.ToString("r"));
|
| Thanks,
| Mike
|
|
|
 
Hi,

Thanks for the response. I understand that the Date header is assigned at
runtime. Is there a way to include it in the headers sent by
HTTPWebRequest? It is not currently being sent. Do I have to create a
class derived from HTTPWebRequest to send it?

Thanks,
Mike
 
Thanks for your response Mike,

For the HTTPwebRequest, I'm afraid the current implementation has hidden
the Date Header's generation internally and dosn't expose any means for us
to intercepting the header collection. If you do need to manually involve
such custom headers in your HTTP communication(base on the RFC of HTTP 1.1
, date header is not required in client request message), I think we may
need to write our own HTTP message/header generating class rather than use
the HttpWebRequest, also I don't think derived class from it can intercept
the header generation.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


--------------------
| From: "Mike Koerner" <[email protected]>
| References: <#[email protected]>
<[email protected]>
| Subject: Re: HTTPWebRequest Date Header
| Date: Mon, 19 Sep 2005 08:50:15 -0700
| Lines: 83
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.3790.1830
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.1830
| X-RFC2646: Format=Flowed; Original
| Message-ID: <#[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: 12.175.2.2
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:125559
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Hi,
|
| Thanks for the response. I understand that the Date header is assigned
at
| runtime. Is there a way to include it in the headers sent by
| HTTPWebRequest? It is not currently being sent. Do I have to create a
| class derived from HTTPWebRequest to send it?
|
| Thanks,
| Mike
|
|
| | > Hi Mike,
| >
| > Welcome to ASPNET newsgroup.
| > As for the "Date" http header you mentioned, based on the
HttpWebRequest's
| > Documentation, it is not included in the headers collection , also
there
| > is
| > no property in the HttpWebRequest class for us to manually adjust it
since
| > it is automatically assigned to the current Date by runtime.
| >
| > #HttpWebRequest.Headers Property
| >
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
| > frlrfsystemnethttpwebrequestclassheaderstopic.asp
| >
| > Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| >
| >
| >
| >
| >
| > --------------------
| > | From: "Mike Koerner" <[email protected]>
| > | Subject: HTTPWebRequest Date Header
| > | Date: Fri, 16 Sep 2005 15:58:48 -0700
| > | Lines: 19
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.3790.1830
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.1830
| > | X-RFC2646: Format=Flowed; Original
| > | Message-ID: <#[email protected]>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | NNTP-Posting-Host: 12.175.2.2
| > | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| > | Xref: TK2MSFTNGXA01.phx.gbl
| > microsoft.public.dotnet.framework.aspnet:125237
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > |
| > | Hi,
| > |
| > | I am having problems setting the HttpWebRequest Date header. I
| > understand
| > | that it is a restricted header and I do receive the
| > | "This header must be modified with the appropriate property." Is
there
| > a
| > | way to make sure that the date header is sent over or a way to work
| > around
| > | this exception?
| > |
| > | Here's a sample of the code:
| > |
| > | System.Net.HttpWebRequest wr;
| > | wr = (HttpWebRequest)WebRequest.Create(requestUrl);
| > | System.Net.WebHeaderCollection whcHeaders = wr.Headers;
| > | whcHeaders.Add(@"Date" , DateTime.Now.ToString("r"));
| > |
| > | Thanks,
| > | Mike
| > |
| > |
| > |
| >
|
|
|
 
Hi,

Thanks for trying to help me out.

I worked around the HTTPWebRequest Date header limitation by implementing a
HTTP TCPClient. I don't need anything fancy, just posting data to a URL
with specific headers. Here's a sample:

string requestUrl = "http://localhost/MyTestPost.aspx";
System.Uri uri = new Uri(requestUrl);
string server = uri.Host;
Int32 port = uri.Port;

System.Net.Sockets.TcpClient client = new
System.Net.Sockets.TcpClient(server, port);
string requestMethod = "POST " + uri.LocalPath + " HTTP/1.0\r\n";
string requestHeaders = "";
string payload = "MyTest\r\n";

requestHeaders += "Date: " + DateTime.Now.ToString("r") + "\r\n";
requestHeaders += "\r\n";

Byte[] data = System.Text.Encoding.ASCII.GetBytes(requestMethod +
requestHeaders + payload);
// Send the message to the connected TcpServer.
NetworkStream stream = client.GetStream();
stream.Write(data, 0, data.Length);
stream.Flush();

string responseData="";

// Receive the TcpServer.response.
for(int i=0;i<100;i++)
{
if (stream.DataAvailable)
{
break;
}
System.Threading.Thread.Sleep(100);
}

Byte[] bytes = new byte[1024];
System.Text.StringBuilder sb = new System.Text.StringBuilder();
while (stream.DataAvailable)
{
int count = stream.Read(bytes, 0, 1024);
if (count == 0)
{
break;
}
sb.Append(System.Text.Encoding.UTF8.GetString(bytes, 0, count));
}


responseData = sb.ToString();
// Close everything.
client.Close();
 
Thanks for your further followup.

That's cool to utilize the existing TcpCliet class.
Thanks for sharing your code with us.

Also, if there're any thing else we can help later, please feel free to
post here.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| From: "Mike Koerner" <[email protected]>
| References: <#[email protected]>
| Subject: Re: HTTPWebRequest Date Header
| Date: Tue, 20 Sep 2005 10:05:46 -0700
| Lines: 81
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.3790.1830
| X-RFC2646: Format=Flowed; Response
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.1830
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: 12.175.2.2
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:125864
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Hi,
|
| Thanks for trying to help me out.
|
| I worked around the HTTPWebRequest Date header limitation by implementing
a
| HTTP TCPClient. I don't need anything fancy, just posting data to a URL
| with specific headers. Here's a sample:
|
| string requestUrl = "http://localhost/MyTestPost.aspx";
| System.Uri uri = new Uri(requestUrl);
| string server = uri.Host;
| Int32 port = uri.Port;
|
| System.Net.Sockets.TcpClient client = new
| System.Net.Sockets.TcpClient(server, port);
| string requestMethod = "POST " + uri.LocalPath + " HTTP/1.0\r\n";
| string requestHeaders = "";
| string payload = "MyTest\r\n";
|
| requestHeaders += "Date: " + DateTime.Now.ToString("r") + "\r\n";
| requestHeaders += "\r\n";
|
| Byte[] data = System.Text.Encoding.ASCII.GetBytes(requestMethod +
| requestHeaders + payload);
| // Send the message to the connected TcpServer.
| NetworkStream stream = client.GetStream();
| stream.Write(data, 0, data.Length);
| stream.Flush();
|
| string responseData="";
|
| // Receive the TcpServer.response.
| for(int i=0;i<100;i++)
| {
| if (stream.DataAvailable)
| {
| break;
| }
| System.Threading.Thread.Sleep(100);
| }
|
| Byte[] bytes = new byte[1024];
| System.Text.StringBuilder sb = new System.Text.StringBuilder();
| while (stream.DataAvailable)
| {
| int count = stream.Read(bytes, 0, 1024);
| if (count == 0)
| {
| break;
| }
| sb.Append(System.Text.Encoding.UTF8.GetString(bytes, 0, count));
| }
|
|
| responseData = sb.ToString();
| // Close everything.
| client.Close();
|
|
| | > Hi,
| >
| > I am having problems setting the HttpWebRequest Date header. I
understand
| > that it is a restricted header and I do receive the
| > "This header must be modified with the appropriate property." Is there
a
| > way to make sure that the date header is sent over or a way to work
around
| > this exception?
| >
| > Here's a sample of the code:
| >
| > System.Net.HttpWebRequest wr;
| > wr = (HttpWebRequest)WebRequest.Create(requestUrl);
| > System.Net.WebHeaderCollection whcHeaders = wr.Headers;
| > whcHeaders.Add(@"Date" , DateTime.Now.ToString("r"));
| >
| > Thanks,
| > Mike
| >
|
|
|
 
Back
Top