1
1
namespace AngleSharp . Io . Network
2
2
{
3
+ using AngleSharp . Io . Extensions ;
4
+ using AngleSharp . Network ;
3
5
using System ;
4
6
using System . Collections . Generic ;
5
7
using System . Linq ;
6
8
using System . Net . Http ;
7
9
using System . Threading ;
8
10
using System . Threading . Tasks ;
9
- using AngleSharp . Network ;
10
11
using HttpMethod = System . Net . Http . HttpMethod ;
11
12
12
13
/// <summary>
@@ -29,14 +30,15 @@ public HttpClientRequester(HttpClient client)
29
30
/// Checks if the given protocol is supported.
30
31
/// </summary>
31
32
/// <param name="protocol">
32
- /// The protocol to check for, e.g. http.
33
+ /// The protocol to check for, e.g., http.
33
34
/// </param>
34
35
/// <returns>
35
36
/// True if the protocol is supported, otherwise false.
36
37
/// </returns>
37
38
public Boolean SupportsProtocol ( String protocol )
38
39
{
39
- return protocol . Equals ( "http" , StringComparison . OrdinalIgnoreCase ) || protocol . Equals ( "https" , StringComparison . OrdinalIgnoreCase ) ;
40
+ return protocol . Equals ( "http" , StringComparison . OrdinalIgnoreCase ) ||
41
+ protocol . Equals ( "https" , StringComparison . OrdinalIgnoreCase ) ;
40
42
}
41
43
42
44
/// <summary>
@@ -50,9 +52,10 @@ public Boolean SupportsProtocol(String protocol)
50
52
public async Task < IResponse > RequestAsync ( IRequest request , CancellationToken cancel )
51
53
{
52
54
// create the request message
53
- var method = new HttpMethod ( request . Method . ToString ( ) . ToUpper ( ) ) ;
55
+ var method = new HttpMethod ( request . Method . Stringify ( ) ) ;
54
56
var requestMessage = new HttpRequestMessage ( method , request . Address ) ;
55
57
var contentHeaders = new List < KeyValuePair < String , String > > ( ) ;
58
+
56
59
foreach ( var header in request . Headers )
57
60
{
58
61
// Source:
@@ -65,12 +68,13 @@ public async Task<IResponse> RequestAsync(IRequest request, CancellationToken ca
65
68
if ( request . Content != null && method != HttpMethod . Get && method != HttpMethod . Head )
66
69
{
67
70
requestMessage . Content = new StreamContent ( request . Content ) ;
71
+
68
72
foreach ( var header in contentHeaders )
69
73
requestMessage . Content . Headers . TryAddWithoutValidation ( header . Key , header . Value ) ;
70
74
}
71
75
72
76
// execute the request
73
- var responseMessage = await _client . SendAsync ( requestMessage , cancel ) ;
77
+ var responseMessage = await _client . SendAsync ( requestMessage , cancel ) . ConfigureAwait ( false ) ;
74
78
75
79
// convert the response
76
80
var response = new Response
@@ -82,7 +86,8 @@ public async Task<IResponse> RequestAsync(IRequest request, CancellationToken ca
82
86
83
87
if ( responseMessage . Content != null )
84
88
{
85
- response . Content = await responseMessage . Content . ReadAsStreamAsync ( ) ;
89
+ response . Content = await responseMessage . Content . ReadAsStreamAsync ( ) . ConfigureAwait ( false ) ;
90
+
86
91
foreach ( var pair in responseMessage . Content . Headers )
87
92
response . Headers [ pair . Key ] = String . Join ( ", " , pair . Value ) ;
88
93
}
0 commit comments