Skip to content

Commit 21cab7f

Browse files
Merge pull request #62 from unoplatform/dev/xygu/162211/cors-bypass
added cors bypass for RSS parser sample
2 parents 4c1b7c1 + 30068cc commit 21cab7f

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

Microsoft.Toolkit.Uwp.SampleApp.Shared/SamplePages/RssParser/RssParserPage.xaml.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
4-
54
using System;
65
using System.Collections.ObjectModel;
76
using System.Net.Http;
7+
using System.Threading;
8+
using System.Threading.Tasks;
89
using Microsoft.Toolkit.Parsers.Rss;
910
using Windows.System;
1011
using Windows.UI.Xaml.Controls;
@@ -18,7 +19,7 @@ public sealed partial class RssParserPage : Page
1819
public RssParserPage()
1920
{
2021
this.InitializeComponent();
21-
Loaded += (s, e) => ParseRSS();
22+
Loaded += (s, e) => ParseRSS();
2223
}
2324

2425
public string Url { get; set; } = "https://visualstudiomagazine.com/rss-feeds/news.aspx";
@@ -28,7 +29,11 @@ public async void ParseRSS()
2829
string feed = null;
2930
RSSFeed.Clear();
3031

32+
#if __WASM__
33+
using (var client = new HttpClient(new CorsBypassHandler()))
34+
#else
3135
using (var client = new HttpClient())
36+
#endif
3237
{
3338
try
3439
{
@@ -66,5 +71,24 @@ private async void RSSList_SelectionChanged(object sender, SelectionChangedEvent
6671

6772
RSSList.SelectedItem = null;
6873
}
74+
75+
#if __WASM__
76+
public class CorsBypassHandler : DelegatingHandler
77+
{
78+
public CorsBypassHandler()
79+
{
80+
InnerHandler = new HttpClientHandler();
81+
}
82+
83+
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
84+
{
85+
var builder = new UriBuilder(request.RequestUri);
86+
builder.Host = "cors-anywhere.herokuapp.com";
87+
builder.Path = request.RequestUri.Host + builder.Path;
88+
89+
return base.SendAsync(new HttpRequestMessage(request.Method, builder.Uri), cancellationToken);
90+
}
91+
}
92+
#endif
6993
}
7094
}

0 commit comments

Comments
 (0)