Skip to content

FollowRedirects not working #2045

Closed
Closed

Description

@rawensoft

If 'FollowRedirects = true', the request completion code is 'StatusCode: Found', but 'StatusCode: OK' is expected. Changing 'FollowRedirects = false' does nothing.

var client = new RestClient(new RestClientOptions()
{
     AutomaticDecompression = DecompressionMethods.All,
     FollowRedirects = true,
     MaxRedirects = 10,
     UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36",
     BaseUrl = new Uri("https://steamcommunity.com/market/eligibilitycheck/?goto=%2Fmy%2Ftradeoffers%2Fprivacy"),
     AllowMultipleDefaultParametersWithSameName = true,
     Proxy = proxy,
     RemoteCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; }
});
var container = new CookieContainer();
/*
* Add a steam cookie to container
*/
var request = new RestRequest(string.Empty, Method.Get)  {  CookieContainer = container };
request.AddHeader("Accept", "*/*");
request.AddHeader("DNT", "1");
request.AddHeader("Upgrade-Insecure-Requests", "1");
request.Timeout = 20000;
var response = await client.ExecuteAsync(req);
  • OS: Windows 10 Pro 22H2 19045.2788
  • .NET version 6
  • Version 109 & 110

Same code on version 108.0.3 works as expected.

Activity

  1. alexeyzimarev commented on Apr 4, 2023

    @alexeyzimarev
    Member

    I don't have anything to say about it. RestSharp just sets the AllowAutoRedirect property of HttpClientHandler with the FollowRedirects value. It doesn't do anything further like responding to Found and make consequent calls. It's very strange that it worked in 108 and stopped working in 109...

    I added a simple test and it passes:

        [Fact]
        public void Ensure_follow_redirect() {
            var value   = false;
            var options = new RestClientOptions { FollowRedirects = true, ConfigureMessageHandler = Configure};
            var _  = new RestClient(options);
            value.Should().BeTrue();
    
            HttpMessageHandler Configure(HttpMessageHandler handler) {
                value = ((handler as HttpClientHandler)!).AllowAutoRedirect;
                return handler;
            }
        }
  2. alexeyzimarev commented on Apr 4, 2023

    @alexeyzimarev
    Member

    I also added another test. The test server endpoint:

            _app.MapGet("redirect", () => Results.Redirect("/success", false, true));

    and the test:

    public class RedirectTests {
        readonly RestClient _client;
    
        public RedirectTests(TestServerFixture fixture, ITestOutputHelper output) {
            var options = new RestClientOptions(fixture.Server.Url) {
                FollowRedirects = true
            };
            _client = new RestClient(options);
        }
    
        [Fact]
        public async Task Can_Perform_GET_Async_With_Redirect() {
            const string val = "Works!";
    
            var request = new RestRequest("redirect");
    
            var response = await _client.ExecuteAsync<Response>(request);
            response.StatusCode.Should().Be(HttpStatusCode.OK);
            response.Data!.Message.Should().Be(val);
        }
    
        class Response {
            public string? Message { get; set; }
        }
    }

    It works fine. The request URL in the response also shows the success endpoint instead of redirect.

  3. rawensoft commented on Apr 4, 2023

    @rawensoft
    Author

    I don't know why this happens and I was surprised myself when I saw that in HttpClient 'AllowAutoRedirect: true', but it is not processed. I don't know how better to provide what I see than screenshots. I hope this somehow helps to solve the problem.

    Details

    Parameters of RestRequest
    image

    Parameters of RestResponse
    image
    image

    Parameters of RestClient
    image

    Parameters of HttpClientHandler
    image

  4. alexeyzimarev commented on Apr 4, 2023

    @alexeyzimarev
    Member

    What's the value of the response Location header?

  5. rawensoft commented on Apr 4, 2023

    @rawensoft
    Author

    All headers in response
    image

  6. alexeyzimarev commented on Apr 4, 2023

    @alexeyzimarev
    Member

    Weird. I searched for similar issues and only found out that https->http redirect don't work by design. What I can tell for sure is that nothing has changed with that code between v108 and v109 except using the read-only options in the client, but it doesn't affect anything as the option value is passed along correctly.

    Are you absolutely sure that that's the only change? If you downgrade RestSharp to v108 right now without changing anything else, can you confirm it works as expected?

  7. rawensoft commented on Apr 4, 2023

    @rawensoft
    Author

    i am absolutely sure. All I do to downgrade is move CookieContainer from RestClient to RestRequest, because there is no such option in the client. Now I checked it again on version 108.0.3 and it works correctly, but on version 109.0.0 it doesn't.

  8. rawensoft commented on Apr 4, 2023

    @rawensoft
    Author

    Now I have checked the correct operation of the redirect through my site and it works as expected. Then I did a check through this link https://steamcommunity.com/market/eligibilitycheck/?goto=%2Fmy%2Ftradeoffers%2Fprivacy and it works, but after I added my cookies, the redirect does not work anymore. Here is my test code for adding cookies that are responsible for authorization on the site

    var container = new CookieContainer();
    container.Add(new Cookie("sessionid", session.SessionID, "/", "steamcommunity.com") { Secure = true });
    container.Add(new Cookie("steamLoginSecure", $"{session.SteamID}%7C%7C{session.AccessToken}", "/", "steamcommunity.com") { Secure = true, HttpOnly = true });
    
  9. rawensoft commented on Apr 4, 2023

    @rawensoft
    Author

    I placed url encoded cookies sessionid and steamLoginSecure direct from browser and redirect not working.

  10. alexeyzimarev commented on Apr 5, 2023

    @alexeyzimarev
    Member

    Yeah, it is definitely related to the change in cookie handling. RestSharp stopped using CookieContainer in favour of setting Set-Cookie headers. What's strange is that I added a test with a redirect endpoint and cookies and it also passes. Kind of hard to figure out the issue if I can't reproduce it...

    I looked if I can do manual redirect follow instead of relying on the handler. It seems to work, but it's a substantial change and I don't know if it solves your problem as I can't really reproduce it.

  11. alexeyzimarev commented on Apr 5, 2023

    @alexeyzimarev
    Member

    Ok, I think because we don't use CookieContainer anymore, you get new cookies in the response and they aren't added to the follow up request. I still don't know why it doesn't follow the redirect, but it looks like the reason why things don't work.

  12. rawensoft commented on Apr 5, 2023

    @rawensoft
    Author

    I just realized this too. In the first request, the cookie must be received, and two more requests go along with the new cookie, but, since the cookie is not added, then they are redirected to the page for adding a cookie, and so it will be up to the MaxRedirects or Timeout.

  13. rawensoft commented on Apr 5, 2023

    @rawensoft
    Author

    Now I checked using my HttpClient and HttpClientHandler, in which I added CookieContainer, and everything works as before. Now I have no more questions.

  14. alexeyzimarev commented on Apr 7, 2023

    @alexeyzimarev
    Member

    You can also just tell RestSharp to add cookie container to the handler:

    var options = new RestClientOptions(url) {
        ConfigureMessageHandler = h => {
            h.CookieContainer = container;
            return h;
        }
    };
    var client = new RestClient(options);
  15. rassilon commented on Jul 19, 2023

    @rassilon

    You can also just tell RestSharp to add cookie container to the handler:

    var options = new RestClientOptions(url) {
        ConfigureMessageHandler = h => {
            h.CookieContainer = container;
            return h;
        }
    };
    var client = new RestClient(options);

    Except, if I do that with 110.1.0, reponse.Cookies doesn't contain the expected cookies. Neither does the container supplied to HtpClientHandler.CookieContainer. RestSharpRequest.Cookies doesn't contain the correct data either (after execution, although any asp.net core anti-forgery cookies are there but nowhere else)
    Neither does client.Options.CookieContainer. :(

    Ugh...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    awaiting-feedbackNeed feedback after the issue is fixed in previewbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions