如何指定范围> 2GB的HttpWebRequest的在.NET 3.5 [英] How to specify range >2GB for HttpWebRequest in .NET 3.5

查看:528
本文介绍了如何指定范围> 2GB的HttpWebRequest的在.NET 3.5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立这个类来下载的部分/段/网段的文件。在.NET 4.0中,我可以用这个code到指定的范围内,从下载

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

和它的作品,因为还有很长的过载为的AddRange方法。

当我抬头的.NET 3.5的版本,我的的AddRange()法允许使用 INT 只有实现

可能的解决方法是使用 的AddRange(字符串,INT) 或的 的AddRange(字符串,INT,INT) 方法。由于该类将不得不工作在.NET 3.5,我得去与字符串规范可惜我似乎无法找到任何样品code,显示了如何使用.NET 3.5这个过程指定范围。任何人都可以表演是如何做到这一点?

感谢。

更新

作为第一个code示例中,我写了节目,我想指定一个范围类型的而不是 INT 。使用type INT 允许请求的字节范围只到2GB,但允许请求的字节范围beyong 2GB。

现在的问题因此:?在.NET 3.5中如何指定有关的HttpWebRequest 2GB的字节范围或更高

解决方案

这是在code。通过 Mutant_Fruit 从的 WebRequest.AddRange - ?怎么样文件> 2GB 的展示了如何添加一个范围说明符超过2GB到HttpWebRequest的

  MethodInfo的方法= typeof运算(WebHeaderCollection).GetMethod
                        (AddWithoutValidate,BindingFlags.Instance | BindingFlags.NonPublic可);

HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(http://www.example.com/file.exe);

长时间启动= int32.MaxValue;
长端= int32.MaxValue + 100000;

字符串键=范围;
字符串VAL =的String.Format(字节= {0}  -  {1},开始,结束);

method.Invoke(request.Headers,新的对象[] {键,VAL});
 

我写了这个扩展方法吧。

 #地区Htt​​pWebRequest.AddRange(长)
静态MethodInfo的httpWebRequestAddRangeHelper = typeof运算(WebHeaderCollection).GetMethod
                                        (AddWithoutValidate,BindingFlags.Instance | BindingFlags.NonPublic可);
///<总结>
///添加一个字节范围header,用于一个特定范围,从所请求的数据的开头或结尾的请求。
///< /总结>
///< PARAM NAME =请求>的<看到CREF =System.Web.HttpWebRequest/>添加的范围符上述< /参数>
///&所述; PARAM NAME =开始>将起始或范围的结束点与所述; /参数>
公共静态无效的AddRange(这HttpWebRequest的要求,长开始){request.AddRange(启动,-1L); }

///<总结>添加一个字节范围头一个指定范围内的要求和LT; /总结>
///< PARAM NAME =请求>的<看到CREF =System.Web.HttpWebRequest/>添加的范围符上述< /参数>
///< PARAM NAME =开始>在此开始发送数据的位置和LT; /参数>
///< PARAM NAME =结束>在其停止发送数据的位置< /参数>
公共静态无效的AddRange(这HttpWebRequest的要求,开始长,长端)
{
    如果(要求== NULL)抛出新ArgumentNullException(要求);
    如果(开始℃下)抛出新ArgumentOutOfRangeException(开始,起始字节不能小于0);
    如果(完<开始)结束= -1;

    字符串键=范围;
    字符串VAL =的String.Format(字节= {0}  -  {1},开始,结束== -1:end.ToString());

    httpWebRequestAddRangeHelper.Invoke(request.Headers,新的对象[] {键,VAL});
}
#endregion
 

扩展方法上面需要以下使用指令

 使用的System.Reflection;
使用System.Net;
 

和没有必要,因为有的<一两个重载使用这种这种扩展方法在.NET 4 href="http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.addrange%28v=VS.100%29.aspx"><$c$c>AddRange方法接受的Int64 )作为参数。<​​/ P>

I'm building this class to download files in parts/sections/segments. In .NET 4.0, I can use this code to specify the range to download from

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

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

When I looked up the .NET 3.5 version, I realised the AddRange() method allows using int only.

The possible workaround would be using the AddRange(string, int) or AddRange(string, int, int) methods. Since the class will have to work in .NET 3.5, I'll have to go with the string specification but unfortunately I can't seem to find any sample code that shows how to specify ranges using this procedure in .NET 3.5. Can anyone show be how to do this?

Thanks.

Update

As the first code sample I wrote shows, I would like to specify a range of type long instead of int. Using type int allows requesting for byte ranges only up to 2GB but long allows requesting for byte ranges beyong 2GB.

The question therefore is: How do I specify byte ranges of 2GB or higher on HttpWebRequest in .NET 3.5?

解决方案

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.

这篇关于如何指定范围&GT; 2GB的HttpWebRequest的在.NET 3.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆