Affichage des articles dont le libellé est C#. Afficher tous les articles
Affichage des articles dont le libellé est C#. Afficher tous les articles

mercredi 2 janvier 2013

Don't (blindly) trust in HttpWebResponse.ContentLength

Happy new year to all of you, it's been a long time since I have not posted on my blog.

While doing some HTTP web requests few days ago,I remained stuck with a strange HttpWebResponse.ContentLength == -1 issue with chunked data beyond a certain length.

My searches on the Web didn't give me a clear solution (or I didn't find / see it), so after some tests with Fiddler2, I think I was able to infer an explication, although I can't say I have the answer.

Here is one of the HTTP reponses with a correct Content-Length :


HTTP/1.1 200 OK
Date: Tue, 01 Jan 2013 21:48:42 GMT
Server: Apache
Content-Disposition: attachment; filename="*****.***"
Content-Length: 1537
Content-Type: application/***
Content-Language: fr-FR
Via: 1.1 ***.**********.**
X-Cache: MISS from ***.**********.**
Keep-Alive: timeout=15, max=90
Connection: Keep-Alive

And one with a "corrupted" Content-Length :

HTTP/1.1 200 OK
Date: Tue, 01 Jan 2013 21:48:44 GMT
Server: Apache
Content-Disposition: attachment; filename="*****.***"
Content-Type: application/***
Content-Language: fr-FR
Via: 1.1 ***.**********.**
X-Cache: MISS from ***.**********.**
Keep-Alive: timeout=15, max=86
Connection: Keep-Alive
Content-Length: 13356

I don't know (and don't want to know) why the HTTP server is not returning the headers in the same order each time (caused by chunked data ?), but I suspect that the .NET Framework (4.0) ignore the Content-Length header when it appears after the Connection one (highlighted in the above HTTP responses)

I've just decided to ignore the Content-Length header, letting the MemoryStream dynamically grow, and it works like a charm.

Better idea or suggestion ?

mardi 21 septembre 2010

Be( a)ware of String.Replace method

If you're using String.Replace method, you will encounter an ArgumentException if oldValue is null or empty.

This may be annoying if you don't check the old value or don't trap this exception.

A radical workaround would be to introduce a little overload with your own static method or by extending the System.String class with a "safe" method like :
/// <summary>
/// Extensions for System.String class
/// </summary>
public static class StringExtensions
{
/// <summary>
/// Safely replace all occurrences of specified System.String instance, with another System.String instance
/// </summary>
public static string SafeReplace(this string extendedString, string oldValue, string newValue)
{
if (string.IsNullOrEmpty(oldValue))
{
return extendedString;
}

return extendedString.Replace(oldValue, newValue);
}
}


samedi 26 avril 2008

Article ranked #2 in CodeProject's ASP.NET "Latest best picks"

Since few days, my second article on CodeProject (ASP.NET Static Map WebControl) is ranked #2 in CodeProject's ASP.NET "Latest best picks" with about 4,8 / 5 (http://www.codeproject.com)

Thanks for all voting members !

A major update of the article (including class and test project) will occurs next week, including support for latest Static Maps API features (image format, paths, etc.) and full featured ASPX tag (center, markers and path)

samedi 19 avril 2008

ASP.NET Session Helper

I'm back to announce the release of a third article on Code Project.

This time, I've leaved WebControls to focus on the ASP.NET Session who was to raw for me. Here is the SessionHandler class, letting you categorize and scope you session variables.

More here : http://www.codeproject.com/KB/aspnet/AspNetSessionHelper.aspx