How to specify range >2GB for HttpWebRequest in .NET 3.5

独自空忆成欢 提交于 2019-12-01 09:14:13

This is the code by Mutant_Fruit copied from WebRequest.AddRange - what about files > 2gb? showing how to to add a range specifier longer than 2GB to an HttpWebRequest.

MethodInfo method = typeof(WebHeaderCollection).GetMethod
                        ("AddWithoutValidate", BindingFlags.Instance | BindingFlags.NonPublic);

HttpWebRequest request = (HttpWebRequest) WebRequest.Create ("http://www.example.com/file.exe");

long start = int32.MaxValue;
long end = int32.MaxValue +  100000;

string key = "Range";
string val = string.Format ("bytes={0}-{1}", start, end);

method.Invoke (request.Headers, new object[] { key, val });

I wrote this extension method for it.

#region HttpWebRequest.AddRange(long)
static MethodInfo httpWebRequestAddRangeHelper = typeof(WebHeaderCollection).GetMethod
                                        ("AddWithoutValidate", BindingFlags.Instance | BindingFlags.NonPublic);
/// <summary>
/// Adds a byte range header to a request for a specific range from the beginning or end of the requested data.
/// </summary>
/// <param name="request">The <see cref="System.Web.HttpWebRequest"/> to add the range specifier to.</param>
/// <param name="start">The starting or ending point of the range.</param>
public static void AddRange(this HttpWebRequest request, long start) { request.AddRange(start, -1L); }

/// <summary>Adds a byte range header to the request for a specified range.</summary>
/// <param name="request">The <see cref="System.Web.HttpWebRequest"/> to add the range specifier to.</param>
/// <param name="start">The position at which to start sending data.</param>
/// <param name="end">The position at which to stop sending data.</param>
public static void AddRange(this HttpWebRequest request, long start, long end)
{
    if (request == null) throw new ArgumentNullException("request");
    if (start < 0) throw new ArgumentOutOfRangeException("start", "Starting byte cannot be less than 0.");
    if (end < start) end = -1;

    string key = "Range";
    string val = string.Format("bytes={0}-{1}", start, end == -1 ? "" : end.ToString());

    httpWebRequestAddRangeHelper.Invoke(request.Headers, new object[] { key, val });
}
#endregion

The extension methods above need the following using directives

using System.Reflection;
using System.Net;

And there is no need to use this this extension method in .NET 4 because there are two overloads of the AddRange method that accept int64 (long) as parameters.

I believe the documentation gives some insight into that:

An example of a Range header in an HTTP protocol request that requests the first 100 bytes would be would be the following:

Range: bytes=-99\r\n\r\n

For this example, the rangeSpecifier parameter would be specified as "bytes" and the range parameter would be -99.

Then there is also this from Wikipedia:

Range
Request only part of an entity. Bytes are numbered from 0.
Range: bytes=500-999

So the code that you would result on the example above is

HttpWebRequest.AddRange("bytes", 500, 999);

If I understand you correctly, you do not have enough int and long. But operator overloading only int?

Use headers.

httpWebRequest.Headers.Add("Range", "bytes=500-999");
Yannick MOLINET

Since .NET 4, HttpWebRequest.AddRange() supports long: http://msdn.microsoft.com/en-us/library/dd992108.aspx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!