Today I wanted to program an application that downloads a file from my website. I wanted it to continue the download when the computer is restarted. So I tried to use

HttpWebRequest.AddRange(int32);

But it only allows int32, not usable for larger files. So I tried to set the headers using

HttpWebRequest.Headers.Add("Range", "bytes="+startbyte.ToString()+"-");

This didn’t work. I received following error message: This header must be modified using the appropriate property. (Dieser Header muss mit der entsprechenden Eigenschaft geändert werden.) This was very annoying. I looked in the internet for a solution but I did not found any except one on an English page:

System.Reflection.MethodInfo method = typeof(System.Net.WebHeaderCollection).GetMethod("AddWithoutValidate", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
                method.Invoke(HttpRequest.Headers, new object[]{"Range", "bytes="+startbyte.ToString()+"-"});

This code allowed me to set a large range using a long integer.

« »