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

I create this class to upload files in parts / sections / segments. In .NET 4.0, I can use this code to specify the range to load from

long startPos = int.MaxValue+1;
HttpWebRequest.AddRange(startPos);

and this works because there is a long overload for the AddRange method.

When I looked at .NET 3.5, I realized that the method AddRange()only allowed use int.

A possible workaround would be to use AddRange(string, int)or AddRange(string, int, int). Since the class should work in .NET 3.5, I have to go with the string specification, but unfortunately I can not find any example code that shows how to specify ranges using this procedure in .NET 3.5. Can anyone show how to do this?

Thank.

Update

, , long int. int 2 , long 2 .

: 2 HttpWebRequest .NET 3.5?

+3
4

Mutant_Fruit, WebRequest.AddRange - > 2gb?, , 2 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 });

.

#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

using

using System.Reflection;
using System.Net;

.NET 4, AddRange, int64 (long) .

+5

, , :

HTTP-, 100 :

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

rangeSpecifier "", be -99.

Wikipedia:

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

, , ,

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

, int . int?

.

httpWebRequest.Headers.Add("Range", "bytes=500-999");
0
source

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

0
source

All Articles