I'm working with a Blazor wasm ASP.Net Core Hosted app.
The following code returns STATUS 200 ok but the string content is empty.
I'm making the request from the client side on the OnInitializedAsync methode of a component.
string URLString = "https://www.bnr.ro/nbrfxrates.xml";
var request = new HttpRequestMessage(HttpMethod.Get, URLString);
request.SetBrowserRequestMode(BrowserRequestMode.NoCors);
request.SetBrowserRequestCache(BrowserRequestCache.NoStore);
HttpResponseMessage response = await Http.SendAsync(request);
string xmlData = await response.Content.ReadAsStringAsync();
Console.WriteLine(xmlData);
UPDATE
I've changed to BrowserRequestMode.Cors
string URLString = "https://www.bnr.ro/nbrfxrates.xml";
var request = new HttpRequestMessage(HttpMethod.Get, URLString);
request.SetBrowserRequestMode(BrowserRequestMode.Cors);
HttpResponseMessage response = await Http.SendAsync(request);
string xmlData = await response.Content.ReadAsStringAsync();
Console.WriteLine(xmlData);
On Server side Program.cs i've added CORS
builder.Services.AddCors(options =>
{
options.AddPolicy("NewPolicy", builder =>
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
});
...
app.UseCors("NewPolicy");
Now the request is blocked
Acccess to fetch at 'https://www.bnr.ro/nbrfxrates.xml' from origin 'https://localhost:7271' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled. dotnet.7.0.4.fmdigzwz3p.js:5 GET https://www.bnr.ro/nbrfxrates.xml net::ERR_FAILED 200 (OK)