I’m pretty sure (or maybe I’m wrong) that we seldom need to avoid following redirects, but I came across such a need one or two times, so I might as well write about it :)
Accompanying source code here.
So, for a concrete scenario where I actually needed the
HttpClient not to follow redirects.
At work we were implementing a new feature that was dependant on a legacy system. This feature consisted in showing some banners (similar to ads) that could be clicked. When a banner is clicked it has to do a bunch of things:
- Open a modal window to do some stuff
- Ping a tracking url indicating that the banner was clicked
- The tracking url may or may not do a redirect, in which case a new tab should be open
Let’s disregard the usability of this… those were the requirements.
So for the above steps, the problematic one was the third one - the legacy system we were integrating with is the one responsible for the banners and tracking urls, so we had to make it work with what we had. The first idea that came to mind was, well, always open a new tab, if it redirects then we’re good, if not… we have an open blank tab :)
Blank tabs aren’t very cool so we thought we could do better. After some more or less complicated ideas, the new plan was to create an indirection1 that would make the request to the tracking url without following the redirect - if the response was a 200, we’re done, if it was a 302 this middleman would return the target url and the application could open it in a new tab.
To do this in .NET we’re using as usual an
HttpClient, but as its default behavior is to follow redirects, a little configuration was required.
And that’s it! If you’re reading the article just to know the required configuration, this is it - so many words to end with about ten of lines of code… sorry :)
Now for the complete context, we’re doing this in a ASP.NET application, and by now one should be aware that we can’t go around instantiating http clients everywhere or we’ll end up with port exhaustion, so we ended up creating a class to abstract this.
And configured as singleton in the DI container.
ServicePointManager configurations to make sure DNS refreshes as seen here.
Finally the controller action could simply do something like the following.
Bonus round: same solution ASP.NET Core 2.1 version
With ASP.NET Core 2.1 there is some new stuff to work with
HttpClient and avoid all these shenanigans because of port exhaustion and DNS refreshes, so I added to the sample a V2 solution using the new
And to register with the DI container:
Other reading material
For more info on ASP.NET Core 2.1
HttpClient related features, Steve Gordon has a series of posts, the first one here.
Ok, this post is probably bigger than it needed to, just to tell how to configure an http client not to follow redirects, but as I had a real world scenario for its usefulness, I thought I might as well share it.
On a side note, the
HttpClientHandler has some other options besides the
AllowAutoRedirect I used, so if you’re needing to do something that
HttpClient doesn’t appear to do directly, you may want to take a look at what the
Thanks for reading!