diff options
Diffstat (limited to 'win/CS/HandBrakeWPF/Utilities/HttpRequestBase.cs')
-rw-r--r-- | win/CS/HandBrakeWPF/Utilities/HttpRequestBase.cs | 54 |
1 files changed, 41 insertions, 13 deletions
diff --git a/win/CS/HandBrakeWPF/Utilities/HttpRequestBase.cs b/win/CS/HandBrakeWPF/Utilities/HttpRequestBase.cs index cc2898b43..fd23565e3 100644 --- a/win/CS/HandBrakeWPF/Utilities/HttpRequestBase.cs +++ b/win/CS/HandBrakeWPF/Utilities/HttpRequestBase.cs @@ -10,6 +10,7 @@ namespace HandBrakeWPF.Utilities { using System; + using System.Net; using System.Net.Http; using System.Text; using System.Threading.Tasks; @@ -20,11 +21,14 @@ namespace HandBrakeWPF.Utilities public class HttpRequestBase { - protected readonly JsonSerializerSettings jsonNetSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; - protected HttpClient client = new HttpClient(); protected string serverUrl; + protected int port; + protected string base64Token; + + private readonly JsonSerializerSettings jsonNetSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; + public async Task<ServerResponse> MakeHttpJsonPostRequest(string urlPath, string json) { if (string.IsNullOrEmpty(json)) @@ -32,14 +36,26 @@ namespace HandBrakeWPF.Utilities throw new InvalidOperationException("No Post Values Found."); } - StringContent content = new StringContent(json, Encoding.UTF8, "application/json"); - HttpResponseMessage response = await client.PostAsync(this.serverUrl + urlPath, content); - if (response != null) + using (HttpClient client = new HttpClient()) { - string returnContent = await response.Content.ReadAsStringAsync(); - ServerResponse serverResponse = new ServerResponse(response.IsSuccessStatusCode, returnContent); + HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, this.serverUrl + urlPath); + if (!string.IsNullOrEmpty(this.base64Token)) + { + requestMessage.Headers.Add("token", this.base64Token); + } + + requestMessage.Content = new StringContent(json, Encoding.UTF8, "application/json"); - return serverResponse; + using (HttpResponseMessage response = await client.SendAsync(requestMessage)) + { + if (response != null) + { + string returnContent = await response.Content.ReadAsStringAsync(); + ServerResponse serverResponse = new ServerResponse(response.IsSuccessStatusCode, returnContent); + + return serverResponse; + } + } } return null; @@ -47,13 +63,25 @@ namespace HandBrakeWPF.Utilities public async Task<ServerResponse> MakeHttpGetRequest(string urlPath) { - HttpResponseMessage response = await client.GetAsync(this.serverUrl + urlPath); - if (response != null) + using (HttpClient client = new HttpClient()) { - string returnContent = await response.Content.ReadAsStringAsync(); - ServerResponse serverResponse = new ServerResponse(response.IsSuccessStatusCode, returnContent); + HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, this.serverUrl + urlPath); + if (!string.IsNullOrEmpty(this.base64Token)) + { + requestMessage.Headers.Add("token", this.base64Token); + } + + using (HttpResponseMessage response = await client.SendAsync(requestMessage)) + { + if (response != null) + { + string returnContent = await response.Content.ReadAsStringAsync(); + ServerResponse serverResponse = null; + serverResponse = response.StatusCode == HttpStatusCode.Unauthorized ? new ServerResponse(false, returnContent) : new ServerResponse(response.IsSuccessStatusCode, returnContent); - return serverResponse; + return serverResponse; + } + } } return null; |