1
1
using System . Collections . Generic ;
2
+ using System . Diagnostics . CodeAnalysis ;
2
3
using System . Net . Http ;
3
4
using System . Threading . Tasks ;
4
5
@@ -8,6 +9,8 @@ namespace BizHawk.Client.Common
8
9
{
9
10
public sealed class HttpCommunication
10
11
{
12
+ private const string MIME_FORM_URLENC = "application/x-www-form-urlencoded" ;
13
+
11
14
private readonly HttpClient _client = new HttpClient ( ) ;
12
15
13
16
private readonly Func < byte [ ] > _takeScreenshotCallback ;
@@ -30,6 +33,15 @@ public HttpCommunication(Func<byte[]> takeScreenshotCallback, string getURL, str
30
33
_client . DefaultRequestHeaders . UserAgent . ParseAdd ( VersionInfo . UserAgentEscaped ) ;
31
34
}
32
35
36
+ private HttpContent ContentObjectFor ( string payload , [ ConstantExpected ] string mimeType )
37
+ => mimeType switch
38
+ {
39
+ MIME_FORM_URLENC => new FormUrlEncodedContent ( [ new ( "payload" , payload ) ] ) ,
40
+ #pragma warning disable BHI1005 // exception type
41
+ _ => throw new NotImplementedException ( )
42
+ #pragma warning restore BHI1005
43
+ } ;
44
+
33
45
public string ExecGet ( string url = null ) => Get ( url ?? GetUrl ) . Result ;
34
46
35
47
/// <inheritdoc cref="ExecPostAsForm"/>
@@ -46,7 +58,7 @@ public string ExecPostAsForm(string url = null, string payload = "")
46
58
{
47
59
return Post (
48
60
url ?? PostUrl ,
49
- new FormUrlEncodedContent ( new Dictionary < string , string > { [ " payload" ] = payload } ) ,
61
+ ContentObjectFor ( payload : payload , mimeType : MIME_FORM_URLENC ) ,
50
62
sendAdvanceRequest : payload . Length >= ExpectContinueThreshold
51
63
) . Result ;
52
64
}
@@ -64,7 +76,7 @@ public async Task<string> Get(string url)
64
76
return null ;
65
77
}
66
78
67
- public async Task < string > Post ( string url , FormUrlEncodedContent content , bool sendAdvanceRequest = false )
79
+ public async Task < string > Post ( string url , HttpContent content , bool sendAdvanceRequest = false )
68
80
{
69
81
_client . DefaultRequestHeaders . ConnectionClose = true ;
70
82
_client . DefaultRequestHeaders . ExpectContinue = sendAdvanceRequest ;
0 commit comments