Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added cors bypass for RSS parser sample #62

Merged
merged 1 commit into from
Sep 30, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}