2
2
using System . Diagnostics ;
3
3
using System . IO ;
4
4
using System . Net ;
5
+ using System . Text ;
5
6
using System . Threading ;
6
7
7
8
namespace GitHub . Unity
@@ -100,7 +101,7 @@ public static WebResponse GetResponseWithoutException(this WebRequest request)
100
101
}
101
102
}
102
103
103
- class DownloadTask : TaskBase < DownloadResult >
104
+ class DownloadTask : TaskBase < DownloadResult >
104
105
{
105
106
private IFileSystem fileSystem ;
106
107
private long bytes ;
@@ -121,10 +122,11 @@ public DownloadTask(CancellationToken token, IFileSystem fileSystem, string url,
121
122
protected override DownloadResult RunWithReturn ( bool success )
122
123
{
123
124
base . RunWithReturn ( success ) ;
124
-
125
+
125
126
RaiseOnStart ( ) ;
126
127
127
- var downloadResult = new DownloadResult {
128
+ var downloadResult = new DownloadResult
129
+ {
128
130
Url = Url ,
129
131
Destination = Destination
130
132
} ;
@@ -213,10 +215,10 @@ public bool Download()
213
215
}
214
216
}
215
217
216
- var responseLength = webResponse . ContentLength ;
218
+ var responseLength = webResponse . ContentLength ;
217
219
if ( restarted && bytes > 0 )
218
220
{
219
- UpdateProgress ( bytes / ( float ) responseLength ) ;
221
+ UpdateProgress ( bytes / ( float ) responseLength ) ;
220
222
}
221
223
222
224
using ( var responseStream = webResponse . GetResponseStream ( ) )
@@ -236,4 +238,44 @@ public bool Download()
236
238
237
239
protected string Destination { get ; }
238
240
}
241
+
242
+ class DownloadTextTask : TaskBase < string >
243
+ {
244
+ public float Progress { get ; set ; }
245
+
246
+ public DownloadTextTask ( CancellationToken token , string url )
247
+ : base ( token )
248
+ {
249
+ Url = url ;
250
+ Name = "DownloadTask" ;
251
+ }
252
+
253
+ protected override string RunWithReturn ( bool success )
254
+ {
255
+ base . RunWithReturn ( success ) ;
256
+
257
+ RaiseOnStart ( ) ;
258
+
259
+ var webRequest = WebRequest . Create ( Url ) ;
260
+ webRequest . Method = "GET" ;
261
+ webRequest . Timeout = 3000 ;
262
+
263
+ using ( var webResponse = ( HttpWebResponse ) webRequest . GetResponseWithoutException ( ) )
264
+ {
265
+ var webResponseCharacterSet = webResponse . CharacterSet ?? Encoding . UTF8 . BodyName ;
266
+ var encoding = Encoding . GetEncoding ( webResponseCharacterSet ) ;
267
+
268
+ using ( var responseStream = webResponse . GetResponseStream ( ) )
269
+ using ( var reader = new StreamReader ( responseStream , encoding ) )
270
+ return reader . ReadToEnd ( ) ;
271
+ }
272
+ }
273
+
274
+ protected virtual void UpdateProgress ( float progress )
275
+ {
276
+ Progress = progress ;
277
+ }
278
+
279
+ protected string Url { get ; }
280
+ }
239
281
}
0 commit comments