How can I get System.Net.Http.HttpClient to not follow 302 redirects?

后端 未结 2 1316
隐瞒了意图╮
隐瞒了意图╮ 2020-12-06 04:41

Using HttpClient from NuGet.

The app sends a post with client.PostAsync(). I\'d like it to NOT follow 302 redirects.

how?

I figure I can just set

2条回答
  •  悲哀的现实
    2020-12-06 05:01

    To add to Hans' answer:

    WebRequestHandler derives from HttpClientHandler but adds properties that generally only are available on full .NET. The WebRequestHandler is not included in the System.Net.Http DLL but rather in System.Net.Http.WebRequest DLL so you have to explicitly include that as a reference in order to see it. Otherwise it won’t show up.

    You can just spring for HttpClientHandler if you don't want to add new DLLs:

        HttpClientHandler clientHandler = new HttpClientHandler();
        clientHandler.AllowAutoRedirect = false;
    

    Reference: https://blogs.msdn.microsoft.com/henrikn/2012/08/07/httpclient-httpclienthandler-and-webrequesthandler-explained/

提交回复
热议问题