Set HttpClient timeout to infinite

This commit is contained in:
Lucas Bortoli
2022-06-27 08:26:11 -03:00
parent caae5decb7
commit b66620c9da

View File

@@ -7,6 +7,7 @@ using System.Linq;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Web; using System.Web;
@@ -28,14 +29,18 @@ namespace ntfysh_client
httpClient = new HttpClient(); httpClient = new HttpClient();
subscribedTopics = new Dictionary<string, StreamReader>(); subscribedTopics = new Dictionary<string, StreamReader>();
httpClient.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);
ServicePointManager.DefaultConnectionLimit = 100; ServicePointManager.DefaultConnectionLimit = 100;
} }
public async Task SubscribeToTopic(string topicId) public async Task SubscribeToTopic(string topicId)
{ {
var stream = await httpClient.GetStreamAsync($"https://ntfy.sh/{HttpUtility.UrlEncode(topicId)}/json"); HttpRequestMessage msg = new HttpRequestMessage(HttpMethod.Get, $"https://ntfy.sh/{HttpUtility.UrlEncode(topicId)}/json");
using (var response = await httpClient.SendAsync(msg, HttpCompletionOption.ResponseHeadersRead))
using (StreamReader reader = new StreamReader(stream)) {
using (var body = await response.Content.ReadAsStreamAsync())
{
using (StreamReader reader = new StreamReader(body))
{ {
subscribedTopics.Add(topicId, reader); subscribedTopics.Add(topicId, reader);
@@ -59,7 +64,8 @@ namespace ntfysh_client
} }
} }
} }
} catch(Exception ex) }
catch (Exception ex)
{ {
Debug.WriteLine(ex); Debug.WriteLine(ex);
@@ -72,6 +78,8 @@ namespace ntfysh_client
} }
} }
} }
}
}
public void RemoveTopic(string topicId) public void RemoveTopic(string topicId)
{ {