2011-07-04 17 views
3

我正在構建此類以便以部分/節/段來下載文件。在.NET 4.0中,我可以使用此代碼來指定範圍從如何在.NET 3.5中爲HttpWebRequest指定範圍> 2GB

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

下載和它的作品,因爲沒有爲方法的AddRange長期超負荷。

當我查找.NET 3.5版本時,我意識到AddRange()方法只允許使用int

可能的解決方法是使用AddRange(string, int)AddRange(string, int, int)方法。由於該類將不得不在.NET 3.5中工作,我將不得不使用字符串規範,但不幸的是我似乎無法找到任何示例代碼,它顯示瞭如何使用.NET 3.5中的此過程指定範圍。任何人都可以顯示如何做到這一點?

謝謝。

更新

如在第一代碼示例我寫節目,我想以指定範圍long代替int類型。使用類型int允許請求僅高達2GB的字節範圍,但是long允許請求字節範圍2GB。

因此,問題是:如何在.NET 3.5中的HttpWebRequest上指定2GB或更高的字節範圍?

回答

5

這是通過 Mutant_Fruit示出了如何以大於2GB添加一個範圍指定更長的時間來HttpWebRequest的一個代碼從WebRequest.AddRange - what about files > 2gb?複製。

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; 

而且也沒有必要因爲有AddRange方法的兩個重載接受int64要使用這項擴展方法在.NET 4 (long)作爲參數。

1

相信the documentation給出了一些洞察:

在請求 第一個100個字節將是 HTTP協議請求Range頭部的一個例子是 以下:

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

對於此示例,該rangeSpecifier 參數將被指定爲 「字節」,範圍參數 將被指定爲-99。

然後還有這個來自Wikipedia

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

這樣就可能導致對例如上面的代碼是

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

感謝您的迅速答覆。我想我沒有把我的問題弄得太清楚。我現在更新了這個問題,以反映我想要做的事情。 –

0

如果我理解正確的話,你沒有足夠的int和long。但運算符只能重載int?

使用標題。

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

謝謝。我會試試這個,讓你知道發生了什麼。 –

+0

我遵循你的建議,遇到了一個絆腳石; '範圍'頭不能被添加到httpWebRequest,所以我GOOGLE了我得到的異常[(搜索結果谷歌)](http://goo.gl/KkD7f),我發現[MSDN HttpWebRequest.AddRange頁面上的帖子] (http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.addrange%28v=vs.90%29.aspx)** Kris H **指示我[解決方法] (http://www.codeguru.com/forum/showthread.php?p=1794639)。我已在此問題的答案中發佈瞭解決方法。感謝您指點我正確的方向。 –