From 30068cc489403b4faed1ef08ac6e42c2019b7fe0 Mon Sep 17 00:00:00 2001 From: Xiaoy312 Date: Mon, 23 Sep 2019 11:57:25 -0400 Subject: [PATCH] added cors bypass for RSS parser sample --- .../RssParser/RssParserPage.xaml.cs | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.SampleApp.Shared/SamplePages/RssParser/RssParserPage.xaml.cs b/Microsoft.Toolkit.Uwp.SampleApp.Shared/SamplePages/RssParser/RssParserPage.xaml.cs index eaed4e05842..4300e030c68 100644 --- a/Microsoft.Toolkit.Uwp.SampleApp.Shared/SamplePages/RssParser/RssParserPage.xaml.cs +++ b/Microsoft.Toolkit.Uwp.SampleApp.Shared/SamplePages/RssParser/RssParserPage.xaml.cs @@ -1,10 +1,11 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. - using System; using System.Collections.ObjectModel; using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; using Microsoft.Toolkit.Parsers.Rss; using Windows.System; using Windows.UI.Xaml.Controls; @@ -18,7 +19,7 @@ public sealed partial class RssParserPage : Page public RssParserPage() { this.InitializeComponent(); - Loaded += (s, e) => ParseRSS(); + Loaded += (s, e) => ParseRSS(); } public string Url { get; set; } = "https://visualstudiomagazine.com/rss-feeds/news.aspx"; @@ -28,7 +29,11 @@ public async void ParseRSS() string feed = null; RSSFeed.Clear(); +#if __WASM__ + using (var client = new HttpClient(new CorsBypassHandler())) +#else using (var client = new HttpClient()) +#endif { try { @@ -66,5 +71,24 @@ private async void RSSList_SelectionChanged(object sender, SelectionChangedEvent RSSList.SelectedItem = null; } + +#if __WASM__ + public class CorsBypassHandler : DelegatingHandler + { + public CorsBypassHandler() + { + InnerHandler = new HttpClientHandler(); + } + + protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + var builder = new UriBuilder(request.RequestUri); + builder.Host = "cors-anywhere.herokuapp.com"; + builder.Path = request.RequestUri.Host + builder.Path; + + return base.SendAsync(new HttpRequestMessage(request.Method, builder.Uri), cancellationToken); + } + } +#endif } } \ No newline at end of file