@@ -46,16 +46,23 @@ public WebRequestAppCastDataDownloader()
46
46
47
47
/// <inheritdoc/>
48
48
public string DownloadAndGetAppCastData ( string url )
49
+ {
50
+ return DownloadAndGetAppCastDataAsync ( url ) . GetAwaiter ( ) . GetResult ( ) ;
51
+ }
52
+
53
+ /// <inheritdoc/>
54
+ public async Task < string > DownloadAndGetAppCastDataAsync ( string url )
49
55
{
50
56
_appcastUrl = url ;
51
57
// configure ssl cert link
52
58
ServicePointManager . ServerCertificateValidationCallback += ValidateRemoteCertificate ;
53
- // use HttpClient synchronously: https://stackoverflow.com/a/53529122/3938401
59
+
54
60
var handler = new HttpClientHandler ( ) ;
55
61
if ( RedirectHandler != null )
56
62
{
57
63
handler . AllowAutoRedirect = false ;
58
64
}
65
+
59
66
if ( TrustEverySSLConnection )
60
67
{
61
68
#if NETCORE
@@ -75,49 +82,46 @@ public string DownloadAndGetAppCastData(string url)
75
82
{
76
83
HttpRequestMessage request = new HttpRequestMessage ( HttpMethod . Post , url ) ;
77
84
request . Content = new StringContent ( ExtraJsonData , Encoding . UTF8 , "application/json" ) ;
78
- var postTask = Task . Run ( ( ) => httpClient . SendAsync ( request ) ) ;
79
- postTask . Wait ( ) ;
80
- if ( postTask . Result . IsSuccessStatusCode )
85
+ HttpResponseMessage response = await httpClient . SendAsync ( request ) . ConfigureAwait ( false ) ;
86
+
87
+ if ( response . IsSuccessStatusCode )
81
88
{
82
- var postTaskStream = Task . Run ( ( ) => postTask . Result . Content . ReadAsStreamAsync ( ) ) ;
83
- postTask . Wait ( ) ;
89
+ Stream responseStream = await response . Content . ReadAsStreamAsync ( ) . ConfigureAwait ( false ) ;
90
+
84
91
ServicePointManager . ServerCertificateValidationCallback -= ValidateRemoteCertificate ;
85
- using ( StreamReader reader = new StreamReader ( postTaskStream . Result , GetAppCastEncoding ( ) ) )
92
+ using ( StreamReader reader = new StreamReader ( responseStream , GetAppCastEncoding ( ) ) )
86
93
{
87
- return reader . ReadToEnd ( ) ;
94
+ return await reader . ReadToEndAsync ( ) . ConfigureAwait ( false ) ;
88
95
}
89
96
}
90
97
}
91
98
else
92
99
{
93
100
if ( RedirectHandler != null )
94
101
{
95
- var task = Task . Run ( ( ) => httpClient . GetAsync ( url ) ) ;
96
- task . Wait ( ) ;
97
- var response = task . Result ;
102
+ HttpResponseMessage response = await httpClient . GetAsync ( url ) . ConfigureAwait ( false ) ;
103
+
98
104
if ( ( int ) response . StatusCode >= 300 && ( int ) response . StatusCode <= 399 )
99
105
{
100
106
var redirectURI = response . Headers . Location ;
101
107
if ( RedirectHandler . Invoke ( url , redirectURI . ToString ( ) , response ) )
102
108
{
103
- return DownloadAndGetAppCastData ( redirectURI . ToString ( ) ) ;
109
+ return await DownloadAndGetAppCastDataAsync ( redirectURI . ToString ( ) ) . ConfigureAwait ( false ) ;
104
110
}
105
111
}
106
112
else if ( response . IsSuccessStatusCode )
107
113
{
108
- var readTask = Task . Run ( ( ) => response . Content . ReadAsStringAsync ( ) ) ;
109
- readTask . Wait ( ) ;
110
- return readTask . Result ;
114
+ return await response . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ;
111
115
}
112
116
}
113
- else
117
+ else
114
118
{
115
- var task = Task . Run ( ( ) => httpClient . GetStreamAsync ( url ) ) ;
116
- var responseStream = task . Result ;
119
+ Stream responseStream = await httpClient . GetStreamAsync ( url ) . ConfigureAwait ( false ) ;
120
+
117
121
ServicePointManager . ServerCertificateValidationCallback -= ValidateRemoteCertificate ;
118
122
using ( StreamReader reader = new StreamReader ( responseStream , GetAppCastEncoding ( ) ) )
119
123
{
120
- return reader . ReadToEnd ( ) ;
124
+ return await reader . ReadToEndAsync ( ) . ConfigureAwait ( false ) ;
121
125
}
122
126
}
123
127
}
0 commit comments