Skip to content

Commit

Permalink
System.Net.Http migration to Windows.Web.Http to handle self signed c…
Browse files Browse the repository at this point in the history
…ertificate on RPi (https)
  • Loading branch information
Jérémy Filhoulaud committed Jan 5, 2021
1 parent eab4b15 commit b67a37b
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 37 deletions.
4 changes: 0 additions & 4 deletions Asset.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ScreenlyManager
{
Expand Down
58 changes: 34 additions & 24 deletions Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
using System.Collections.ObjectModel;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Security.Cryptography.Certificates;
using Windows.Storage.AccessCache;
using Windows.Web.Http;
using Windows.Web.Http.Filters;
using Windows.Web.Http.Headers;

namespace ScreenlyManager
{
Expand Down Expand Up @@ -59,24 +62,32 @@ public string HttpLink
{
get
{
return $"http://{IpAddress}:{Port}";
return $"{IpAddress}:{Port}";
}
}

[Newtonsoft.Json.JsonIgnore]
public HttpBaseProtocolFilter HttpFilter { get; set; }

public Device()
{
this.Assets = new List<Asset>();
this.IsUp = false;

this.HttpFilter = new HttpBaseProtocolFilter();
this.HttpFilter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Expired);
this.HttpFilter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted);
this.HttpFilter.IgnorableServerCertificateErrors.Add(ChainValidationResult.InvalidName);
}

public async Task<bool> IsReachable()
{
try
{
HttpClient client = new HttpClient();
client.Timeout = new TimeSpan(0, 0, 3);
HttpClient client = new HttpClient(this.HttpFilter);
var cancellationTokenSource = new CancellationTokenSource(3000);

HttpResponseMessage response = await client.GetAsync(this.HttpLink);
HttpResponseMessage response = await client.GetAsync(new Uri(this.HttpLink)).AsTask(cancellationTokenSource.Token);
if (response == null || !response.IsSuccessStatusCode)
{
this.IsUp = false;
Expand Down Expand Up @@ -111,7 +122,7 @@ public async Task GetAssetsAsync()
try
{
HttpClient request = new HttpClient();
using (HttpResponseMessage response = await request.GetAsync(this.HttpLink + parameters))
using (HttpResponseMessage response = await request.GetAsync(new Uri(this.HttpLink + parameters)))
{
resultJson = await response.Content.ReadAsStringAsync();
}
Expand All @@ -138,7 +149,7 @@ public async Task<bool> RemoveAssetAsync(string assetId)
try
{
HttpClient request = new HttpClient();
using (HttpResponseMessage response = await request.DeleteAsync(this.HttpLink + parameters))
using (HttpResponseMessage response = await request.DeleteAsync(new Uri(this.HttpLink + parameters)))
{
resultJson = await response.Content.ReadAsStringAsync();
}
Expand Down Expand Up @@ -176,9 +187,9 @@ public async Task<Asset> UpdateAssetAsync(Asset a)
try
{
HttpClient client = new HttpClient();
HttpContent content = new ByteArrayContent(data, 0, data.Length);
content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
using (HttpResponseMessage response = await client.PutAsync(this.HttpLink + parameters, content))
HttpBufferContent content = new HttpBufferContent(data.AsBuffer());
content.Headers.ContentType = new HttpMediaTypeHeaderValue("application/x-www-form-urlencoded");
using (HttpResponseMessage response = await client.PutAsync(new Uri(this.HttpLink + parameters), content))
{
resultJson = await response.Content.ReadAsStringAsync();
}
Expand Down Expand Up @@ -220,9 +231,9 @@ public async Task UpdateOrderAssetsAsync(string newOrder)
try
{
HttpClient client = new HttpClient();
HttpContent content = new ByteArrayContent(data, 0, data.Length);
content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
using (HttpResponseMessage response = await client.PostAsync(this.HttpLink + parameters, content))
HttpBufferContent content = new HttpBufferContent(data.AsBuffer());
content.Headers.ContentType = new HttpMediaTypeHeaderValue("application/x-www-form-urlencoded");
using (HttpResponseMessage response = await client.PostAsync(new Uri(this.HttpLink + parameters), content))
{
resultJson = await response.Content.ReadAsStringAsync();
}
Expand Down Expand Up @@ -276,10 +287,9 @@ public async Task CreateAssetAsync(Asset a)
try
{
HttpClient client = new HttpClient();
HttpContent content = new ByteArrayContent(data, 0, data.Length);
content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");

using (HttpResponseMessage response = await client.PostAsync(this.HttpLink + parameters, content))
HttpBufferContent content = new HttpBufferContent(data.AsBuffer());
content.Headers.ContentType = new HttpMediaTypeHeaderValue("application/x-www-form-urlencoded");
using (HttpResponseMessage response = await client.PostAsync(new Uri(this.HttpLink + parameters), content))
{
resultJson = await response.Content.ReadAsStringAsync();
}
Expand Down Expand Up @@ -322,7 +332,7 @@ public async Task<Asset> GetAssetAsync(string assetId)
try
{
HttpClient request = new HttpClient();
using (HttpResponseMessage response = await request.GetAsync(this.HttpLink + parameters))
using (HttpResponseMessage response = await request.GetAsync(new Uri(this.HttpLink + parameters)))
{
resultJson = await response.Content.ReadAsStringAsync();
}
Expand Down Expand Up @@ -352,11 +362,11 @@ public async Task<string> PostFileAssetAsync(byte[] itemToSend, string fileName,
try
{
HttpClient request = new HttpClient();
var content = new MultipartFormDataContent();
var itemContent = new ByteArrayContent(itemToSend);
itemContent.Headers.ContentType = MediaTypeHeaderValue.Parse(contentType);
var content = new HttpMultipartFormDataContent();
HttpBufferContent itemContent = new HttpBufferContent(itemToSend.AsBuffer());
itemContent.Headers.ContentType = HttpMediaTypeHeaderValue.Parse(contentType);
content.Add(itemContent, "file_upload", fileName);
using (HttpResponseMessage response = await request.PostAsync(this.HttpLink + parameters, content))
using (HttpResponseMessage response = await request.PostAsync(new Uri(this.HttpLink + parameters), content))
{
resultJson = await response.Content.ReadAsStringAsync();
}
Expand Down
2 changes: 1 addition & 1 deletion Package.appxmanifest
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" IgnorableNamespaces="uap mp">
<Identity Name="bd4d638c-2e98-49e2-be8a-65be3fa9900d" Publisher="CN=j.filhoulaud" Version="1.0.19.0" />
<Identity Name="bd4d638c-2e98-49e2-be8a-65be3fa9900d" Publisher="CN=j.filhoulaud" Version="1.0.21.0" />
<mp:PhoneIdentity PhoneProductId="bd4d638c-2e98-49e2-be8a-65be3fa9900d" PhonePublisherId="00000000-0000-0000-0000-000000000000" />
<Properties>
<DisplayName>ScreenlyManager</DisplayName>
Expand Down
4 changes: 2 additions & 2 deletions Strings/de-DE/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,11 @@
<comment>AddOrChangeDevicePage.xaml</comment>
</data>
<data name="AddDeviceIp.Text" xml:space="preserve">
<value>IP Adresse</value>
<value>URL</value>
<comment>AddOrChangeDevicePage.xaml</comment>
</data>
<data name="AddDeviceIpTextBox.PlaceholderText" xml:space="preserve">
<value>z.B. 192.168.1.10</value>
<value>z.B. http://192.168.1.10</value>
<comment>AddOrChangeDevicePage.xaml</comment>
</data>
<data name="AddDeviceLocation.Text" xml:space="preserve">
Expand Down
4 changes: 2 additions & 2 deletions Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,11 @@
<comment>AddOrChangeDevicePage.xaml</comment>
</data>
<data name="AddDeviceIp.Text" xml:space="preserve">
<value>IP Address</value>
<value>URL</value>
<comment>AddOrChangeDevicePage.xaml</comment>
</data>
<data name="AddDeviceIpTextBox.PlaceholderText" xml:space="preserve">
<value>Like 192.168.1.10</value>
<value>Like http://192.168.1.10</value>
<comment>AddOrChangeDevicePage.xaml</comment>
</data>
<data name="AddDeviceLocation.Text" xml:space="preserve">
Expand Down
4 changes: 2 additions & 2 deletions Strings/fr-FR/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,11 @@
<comment>AddOrChangeDevicePage.xaml</comment>
</data>
<data name="AddDeviceIp.Text" xml:space="preserve">
<value>Adresse IP</value>
<value>URL</value>
<comment>AddOrChangeDevicePage.xaml</comment>
</data>
<data name="AddDeviceIpTextBox.PlaceholderText" xml:space="preserve">
<value>Par exemple : 192.168.1.10</value>
<value>Par exemple : http://192.168.1.10</value>
<comment>AddOrChangeDevicePage.xaml</comment>
</data>
<data name="AddDeviceLocation.Text" xml:space="preserve">
Expand Down
4 changes: 2 additions & 2 deletions Strings/nl-NL/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,11 @@
<comment>AddOrChangeDevicePage.xaml</comment>
</data>
<data name="AddDeviceIp.Text" xml:space="preserve">
<value>IP-adres</value>
<value>URL</value>
<comment>AddOrChangeDevicePage.xaml</comment>
</data>
<data name="AddDeviceIpTextBox.PlaceholderText" xml:space="preserve">
<value>Zoals 192.168.1.10</value>
<value>Zoals http://192.168.1.10</value>
<comment>AddOrChangeDevicePage.xaml</comment>
</data>
<data name="AddDeviceLocation.Text" xml:space="preserve">
Expand Down

0 comments on commit b67a37b

Please sign in to comment.