views:

598

answers:

3

I'm trying to integrate with an API that requires the "Date" header in a web request. As you may well know, .NET has removed the ability to append or even display a default Date header in your request, so I was wondering about a workaround.

Here's the catch: you can't use TCPClients because I don't have "permissions" on my server for such a thing as sockets. Is there some way to "cheat" the system to fake a date header or to trick it into posting a Date header? Or should I just give up on this API?

Thanks...

(PS, language is VB.NET if you please :) )

A: 

According to the documentation, the system will add a Date header with the current date and time. Do you need a different value for the header?

You may be able to add it by using request.Headers.AddWithoutValidate.

John Saunders
hey again john :) when i pull down all the request headers, the Date is not included. I need to send the date +7hrs to match GMT.
Jason
also, I can't access request.Headers.AddWithoutValidate because it is protected?
Jason
Crap. I thought I had it set to not show me protected members. Sorry. But on the Date, are you sure it sends the wrong thing, that your target won't understand it?
John Saunders
it doesn't send anything at all for the date... the headers i get are: Authorization: Zeep [api]:[hash]Content-Type: application/x-www-form-urlencodedHost: api.zeepmobile.comContent-Length: 42Connection: Keep-Alive
Jason
See http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.framework.aspnet/2005-09/msg03097.html. He worked around the problem by using TCPClient.
John Saunders
I did some research on this, and I see no reason why the current date/time are not sent on every request. I opened https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=475082 about this issue.
John Saunders
wow, thanks... i'm interested to see what they have to say, if they say anything at all... as for the TCPClient, I saw that a while ago, but i can't use that method (as specified in the question)
Jason
I'll believe it when I see it, but they just changed the status on this Connect report to "Fixed".
John Saunders
A: 

This isn't the fastest code in the world, but it might work for a while:

Type type = request.Headers.GetType();
MethodInfo method = type.GetMethod("AddWithoutValidate", 
    BindingFlags.Instance | BindingFlags.NonPublic);
method.Invoke(request.Headers, new [] { "Date", DateTime.Now.ToString() })

Just be aware that using reflection to access private or protected members is extremely fragile and can break with any update to .NET that MS does...

consultutah
From my research, it seems this won't help. They don't even send the header with the current date/time, as documented.
John Saunders
BTW, I'm about to downvote this. Not that it was very bad, but to indicate to future readers that this did not contribute to the solution (since HttpWebRequest wasn't going to send the header no matter whether or not it was set)
John Saunders
Not a problem...
consultutah
Is this fixed in .NET 3.5 - the Date is being send on my code
rotary_engine
Date with no-hack is not working in .NET 3.5, only in 4.0. I have to vote for this as it provides a good temporary solution!
aloneguid
A: 

I found the answer to the date problem, you just have to access the request directly as a HttpWebRequest. It seems like a simple answer but it worked.

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Uri)  ;

request.Date = date;
request.Method = "POST";
Khalid Abuhakmeh
Which .NET runtime supports that?
aloneguid