@@ -1074,18 +1074,40 @@ private static byte[] GetHeaderBytes(string fileName, string mimeType, string fi
10741074 }
10751075
10761076 public static void DownloadFileTo ( this string downloadUrl , string fileName ,
1077- Dictionary < string , string > headers = null )
1077+ List < HttpHeader > headers = null )
10781078 {
10791079 var webClient = new WebClient ( ) ;
10801080 if ( headers != null )
10811081 {
1082- foreach ( var entry in headers )
1082+ foreach ( var header in headers )
10831083 {
1084- webClient . Headers [ entry . Key ] = entry . Value ;
1084+ webClient . Headers [ header . Name ] = header . Value ;
10851085 }
10861086 }
10871087 webClient . DownloadFile ( downloadUrl , fileName ) ;
10881088 }
1089+
1090+ public static void SetRange ( this HttpWebRequest request , long from , long ? to )
1091+ {
1092+ var rangeSpecifier = "bytes" ;
1093+ var curRange = request . Headers [ HttpRequestHeader . Range ] ;
1094+
1095+ if ( string . IsNullOrEmpty ( curRange ) )
1096+ {
1097+ curRange = rangeSpecifier + "=" ;
1098+ }
1099+ else
1100+ {
1101+ if ( string . Compare ( curRange . Substring ( 0 , curRange . IndexOf ( '=' ) ) , rangeSpecifier , StringComparison . OrdinalIgnoreCase ) != 0 )
1102+ throw new NotSupportedException ( "Invalid Range: " + curRange ) ;
1103+ curRange = string . Empty ;
1104+ }
1105+ curRange += from . ToString ( ) ;
1106+ if ( to != null ) {
1107+ curRange += "-" + to ;
1108+ }
1109+ request . Headers [ HttpRequestHeader . Range ] = curRange ;
1110+ }
10891111
10901112 public static void AddHeader ( this HttpWebRequest res , string name , string value ) =>
10911113 res . Headers [ name ] = value ;
@@ -1120,14 +1142,14 @@ public static HttpWebRequest With(this HttpWebRequest httpReq, Action<HttpReques
11201142
11211143 if ( config . Authorization != null )
11221144 httpReq . Headers [ HttpHeaders . Authorization ] =
1123- config . Authorization . Value . Key + " " + config . Authorization . Value . Value ;
1145+ config . Authorization . Name + " " + config . Authorization . Value ;
1146+
1147+ if ( config . Range != null )
1148+ httpReq . SetRange ( config . Range . From , config . Range . To ) ;
11241149
1125- if ( config . Headers != null )
1150+ foreach ( var entry in config . Headers )
11261151 {
1127- foreach ( var entry in config . Headers )
1128- {
1129- httpReq . Headers [ entry . Key ] = entry . Value ;
1130- }
1152+ httpReq . Headers [ entry . Name ] = entry . Value ;
11311153 }
11321154
11331155 return httpReq ;
0 commit comments