When you parse a large number of sites / RSS feeds using Microsoft HTTPClient or WebClient you can get an exception :
”ProtocolViolationException: “The value of the date string in the header is invalid”
This error usually occurs when you try to access LastModified date in a response.
var response = (HttpWebResponse)httpRequest.GetResponse();
var lastModified = response.LastModified;
The problem is usually related where LastModified date is send in incorrect format that ignores the HTTP specs.
Then Microsoft code inside WebClient throws this exception.
This can be very painful but there is a nice workaround for this.
Instead of accessing strongly typed LastModified header property in response get it by reading it from the response header:
var resultedLastModified = response.Headers["Last-Modified"];
After this you’ll need to check for nulls, and try to parse it as datetime but you will not get ugly “The value of the date string in the header is invalid” exception.
If you like this article don’t forget to subscribe to this blog and make sure you don’t miss new upcoming blog posts.