FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
MapWindow5/src/MW5.Shared/DownloadHelper.cs at develop · MapWindow/MapWindow5 · GitHub
MapWindow
/
MapWindow5
Public
Notifications
You must be signed in to change notification settings
Fork
102
Star
308
Code
Issues
14
Pull requests
12
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
MapWindow5
/
src
/
MW5.Shared
/
DownloadHelper.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
73 lines (69 loc) · 2.95 KB
Breadcrumbs
MapWindow5
/
src
/
MW5.Shared
/
DownloadHelper.cs
Copy path
File metadata and controls
73 lines (69 loc) · 2.95 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DownloadHelper.cs" company="MapWindow OSS Team - www.mapwindow.org">
// MapWindow OSS Team - 2015
// </copyright>
// <summary>
// Defines the DownloadHelper type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
using
System
;
using
System
.
IO
;
using
System
.
Net
.
Http
;
using
System
.
Net
.
Http
.
Headers
;
using
System
.
Threading
.
Tasks
;
using
Newtonsoft
.
Json
;
namespace
MW5
.
Shared
{
public
static
class
DownloadHelper
{
/// <summary>
/// Downloads the binary asynchronously.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="filename">The filename.</param>
/// <returns>The task</returns>
public
static
async
Task
DownloadBinaryAsync
(
string
url
,
string
filename
)
{
Logger
.
Current
.
Trace
(
"Start downloading new installer"
)
;
using
(
var
client
=
new
HttpClient
(
)
)
{
using
(
var
response
=
await
client
.
GetAsync
(
url
,
HttpCompletionOption
.
ResponseHeadersRead
)
)
{
using
(
var
streamToReadFrom
=
await
response
.
Content
.
ReadAsStreamAsync
(
)
)
{
using
(
Stream
streamToWriteTo
=
File
.
Open
(
filename
,
FileMode
.
Create
)
)
{
await
streamToReadFrom
.
CopyToAsync
(
streamToWriteTo
)
;
Logger
.
Current
.
Info
(
"New installer is downloaded and saved as "
+
filename
)
;
}
}
}
}
}
/// <summary>
/// Downloads the serialized json data asynchronous.
/// </summary>
/// <typeparam name="T">The type of output we expect our JSON data to be deserialized into</typeparam>
/// <param name="url">The URL.</param>
/// <remarks>http://www.constantinosorphanides.com/950/asynchronous-json-downloader-and-serializer-class-for-c-sharp/</remarks>
/// <returns>The deserialized object</returns>
public
static
async
Task
<
T
>
DownloadSerializedJSONDataAsync
<
T
>
(
string
url
)
where
T
:
new
(
)
{
using
(
var
httpClient
=
new
HttpClient
(
)
)
{
httpClient
.
DefaultRequestHeaders
.
Accept
.
Add
(
new
MediaTypeWithQualityHeaderValue
(
"application/json"
)
)
;
string
jsonData
;
try
{
jsonData
=
await
httpClient
.
GetStringAsync
(
url
)
;
}
catch
(
Exception
ex
)
{
Logger
.
Current
.
Debug
(
"Exception in DownloadSerializedJSONDataAsync: "
+
ex
.
Message
)
;
return
default
(
T
)
;
}
return
!
string
.
IsNullOrEmpty
(
jsonData
)
?
JsonConvert
.
DeserializeObject
<
T
>
(
jsonData
)
:
default
(
T
)
;
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL