Skip to content

Commit

Permalink
added cors bypass for RSS parser sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaoy312 committed Sep 25, 2019
1 parent 4c1b7c1 commit 30068cc
Showing 1 changed file with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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";
Expand All @@ -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
{
Expand Down Expand Up @@ -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<HttpResponseMessage> 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
}
}

0 comments on commit 30068cc

Please sign in to comment.