Skip to content

Commit cc6b417

Browse files
committed
Fix error with mono and httpclient
1 parent 68ee00c commit cc6b417

File tree

14 files changed

+61
-24
lines changed

14 files changed

+61
-24
lines changed
Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
using Microsoft.Extensions.Logging;
22
using Newtonsoft.Json.Linq;
3-
using System;
4-
using System.Net.Http;
3+
using System.IO;
4+
using System.Net;
55
using System.Threading.Tasks;
66

77
namespace Feli.OpenMod.JoinLeaveMessages.Helpers
88
{
9-
public class IpGeolocationHelper : IDisposable
9+
public class IpGeolocationHelper
1010
{
1111
private readonly ILogger<IpGeolocationHelper> _logger;
12-
private readonly HttpClient _client;
1312

1413
public IpGeolocationHelper(ILogger<IpGeolocationHelper> logger)
1514
{
1615
_logger = logger;
17-
_client = new HttpClient();
1816
}
1917

2018
public async Task<string> GetCountryFromIpAsync(string address)
2119
{
22-
var response = await _client.GetAsync($"http://ip-api.com/json/{address}?fields=status,message,country");
20+
var request = CreateRequest(address);
2321

24-
if (!response.IsSuccessStatusCode)
22+
var response = (HttpWebResponse)await request.GetResponseAsync();
23+
24+
if (response.StatusCode != HttpStatusCode.OK)
2525
{
2626
_logger.LogError("HTTP Error: {StatusCode}, {StatusCodeString}", (int)response.StatusCode, response.StatusCode.ToString());
2727
return string.Empty;
2828
}
2929

30-
var content = await response.Content.ReadAsStringAsync();
30+
var content = await ReadContentAsync(response.GetResponseStream());
3131

3232
var @object = JObject.Parse(content);
3333

@@ -40,9 +40,16 @@ public async Task<string> GetCountryFromIpAsync(string address)
4040
return @object["country"].ToString();
4141
}
4242

43-
public void Dispose()
43+
internal WebRequest CreateRequest(string address)
4444
{
45-
_client.Dispose();
45+
return WebRequest.Create($"http://ip-api.com/json/{address}?fields=status,message,country");
46+
}
47+
48+
internal async Task<string> ReadContentAsync(Stream stream)
49+
{
50+
using var reader = new StreamReader(stream);
51+
52+
return await reader.ReadToEndAsync();
4653
}
4754
}
4855
}

Feli.RocketMod.JoinLeaveMessages/Feli.RocketMod.JoinLeaveMessages.csproj

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,40 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
12-
<PackageReference Include="OpenMod.UnityEngine.Redist" Version="2019.4.10" />
13-
<PackageReference Include="OpenMod.Unturned.Redist" Version="3.21.33-preview.1" />
1412
</ItemGroup>
1513

1614
<ItemGroup>
15+
<Reference Include="Assembly-CSharp">
16+
<HintPath>..\libs\Assembly-CSharp.dll</HintPath>
17+
</Reference>
18+
<Reference Include="com.rlabrecque.steamworks.net">
19+
<HintPath>..\libs\com.rlabrecque.steamworks.net.dll</HintPath>
20+
</Reference>
1721
<Reference Include="Rocket.API">
18-
<HintPath>C:\Users\felin\Downloads\Rocket.Unturned\Rocket.API.dll</HintPath>
22+
<HintPath>..\libs\Rocket.API.dll</HintPath>
1923
</Reference>
2024
<Reference Include="Rocket.Core">
21-
<HintPath>C:\Users\felin\Downloads\Rocket.Unturned\Rocket.Core.dll</HintPath>
25+
<HintPath>..\libs\Rocket.Core.dll</HintPath>
2226
</Reference>
2327
<Reference Include="Rocket.Unturned">
24-
<HintPath>C:\Users\felin\Downloads\Rocket.Unturned\Rocket.Unturned.dll</HintPath>
28+
<HintPath>..\libs\Rocket.Unturned.dll</HintPath>
29+
</Reference>
30+
<Reference Include="SDG.NetTransport">
31+
<HintPath>..\libs\SDG.NetTransport.dll</HintPath>
2532
</Reference>
2633
<Reference Include="System.Net.Http" />
34+
<Reference Include="SystemEx">
35+
<HintPath>..\libs\SystemEx.dll</HintPath>
36+
</Reference>
37+
<Reference Include="UnityEngine">
38+
<HintPath>..\libs\UnityEngine.dll</HintPath>
39+
</Reference>
40+
<Reference Include="UnityEngine.CoreModule">
41+
<HintPath>..\libs\UnityEngine.CoreModule.dll</HintPath>
42+
</Reference>
43+
<Reference Include="UnityEx">
44+
<HintPath>..\libs\UnityEx.dll</HintPath>
45+
</Reference>
2746
</ItemGroup>
2847

2948
</Project>
Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
using Newtonsoft.Json.Linq;
22
using Rocket.Core.Logging;
3-
using System.Net.Http;
3+
using System.IO;
4+
using System.Net;
45

56
namespace Feli.RocketMod.JoinLeaveMessages.Helpers
67
{
78
public class IpGeolocationHelper
89
{
910
public static string GetCountryFromIp(string address)
1011
{
11-
var client = new HttpClient();
12+
var request = CreateRequest(address);
1213

13-
var response = client.GetAsync($"http://ip-api.com/json/{address}?fields=status,message,country").GetAwaiter().GetResult();
14+
var response = (HttpWebResponse)request.GetResponse();
1415

15-
client.Dispose();
16-
17-
if (!response.IsSuccessStatusCode)
16+
if (response.StatusCode != HttpStatusCode.OK)
1817
{
1918
Logger.LogError($"HTTP Error: {(int)response.StatusCode}, {response.StatusCode}");
2019
return string.Empty;
2120
}
2221

23-
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
22+
var content = ReadContent(response.GetResponseStream());
2423

2524
var @object = JObject.Parse(content);
2625

@@ -32,5 +31,17 @@ public static string GetCountryFromIp(string address)
3231

3332
return @object["country"].ToString();
3433
}
34+
35+
internal static WebRequest CreateRequest(string address)
36+
{
37+
return WebRequest.Create($"http://ip-api.com/json/{address}?fields=status,message,country");
38+
}
39+
40+
internal static string ReadContent(Stream stream)
41+
{
42+
using var reader = new StreamReader(stream);
43+
44+
return reader.ReadToEnd();
45+
}
3546
}
3647
}

Feli.RocketMod.JoinLeaveMessages/Plugin.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected override void Load()
2525
if(Configuration.Instance.LeaveMessages)
2626
U.Events.OnPlayerDisconnected += OnPlayerDisconnected;
2727

28-
Logger.Log($"JoinLeaveMessages plugin v1.0.1 loaded !");
28+
Logger.Log($"JoinLeaveMessages plugin v1.0.2 loaded !");
2929
Logger.Log("Do you want more cool plugins? Join now: https://discord.gg/4FF2548 !");
3030
}
3131

@@ -55,7 +55,7 @@ protected override void Unload()
5555
U.Events.OnPlayerConnected -= OnPlayerConnected;
5656
U.Events.OnPlayerDisconnected -= OnPlayerDisconnected;
5757

58-
Logger.Log($"JoinLeaveMessages plugin v1.0.0 unloaded !");
58+
Logger.Log($"JoinLeaveMessages plugin v1.0.2 unloaded !");
5959
Logger.Log("Do you want more cool plugins? Join now: https://discord.gg/4FF2548 !");
6060
}
6161
}

libs/Assembly-CSharp.dll

3.36 MB
Binary file not shown.

libs/Rocket.API.dll

15.5 KB
Binary file not shown.

libs/Rocket.Core.dll

75 KB
Binary file not shown.

libs/Rocket.Unturned.dll

94 KB
Binary file not shown.

libs/SDG.NetTransport.dll

10.2 KB
Binary file not shown.

libs/SystemEx.dll

10.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)