tmenier / Flurl

Fluent URL builder and testable HTTP client for .NET

Home Page:https://flurl.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

return 302 "Moved Temporarily" with flurl, but using httpclient is normal

DreamTom opened this issue · comments

using Flurl.Http;

string host = "https://www.hongkongdisneyland.com/zh-cn/merchstore/general/load";
var res = await host.GetAsync();

using (var client = new HttpClient())
{
var res2 = await client.SendAsync(new HttpRequestMessage
{
RequestUri = new Uri("https://www.hongkongdisneyland.com/zh-cn/merchstore/general/load"),
Method = HttpMethod.Get,
});
var resss = await res2.Content.ReadAsStringAsync();
}
Console.ReadKey();

.net version: .net 8.0 windows 11 ConsoleApp

I got the same result as you, until I enabled cookies. At a minimum, if you're just making a single call, do this:

var res = await host.WithCookies(out _).GetAsync();

But if you're making multiple calls you'll want to capture that CookieJar pass it to subsequent calls:

var resp1 = await host.AppendPathSegments(...).WithCookies(out var jar).GetAsync();
var resp2 = await host.AppendPathSegments(...).WithCookies(jar).GetAsync();
...

Or better yet, use a CookieSession. See the docs for details.