Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpClient redirect bug #21446

Closed
XaveScor opened this issue May 1, 2017 · 8 comments
Closed

HttpClient redirect bug #21446

XaveScor opened this issue May 1, 2017 · 8 comments
Labels
area-System.Net.Http question Answer questions and provide assistance, not an issue with source code or documentation.
Milestone

Comments

@XaveScor
Copy link

XaveScor commented May 1, 2017

http://stackoverflow.com/questions/43573321/httpclient-302-redirect
I have 2 urls: https://pcr.apple.com/id868222886 and https://jigsaw.w3.org/HTTP/300/302.html. Both have a location link and 302 response code. But apple's link has 0-len response body.

using System;
using System.IO;
using System.Net.Http;

namespace XaveScor.PodcastFeed
{
    public class RemoteFeedSource: FeedSource
    {
        private string url;
        protected virtual HttpMessageHandler Handler => new HttpClientHandler() { AllowAutoRedirect = true };

        public override Stream Stream => client.Value.GetStreamAsync(url).Result;

        private readonly Lazy<HttpClient> client;

        public RemoteFeedSource(string url)
        {
            client = new Lazy<HttpClient>(() => new HttpClient(Handler), false);    
            this.url = url;
        }
    }
}


[TestMethod]
public void Test1() //fail
{
    var source = new RemoteFeedSource("https://pcr.apple.com/id868222886");
    Assert.AreNotEqual(source.Stream.GetString(), "");
}

[TestMethod]
public void Test2() //success
{
    var source = new RemoteFeedSource("https://jigsaw.w3.org/HTTP/300/302.html");
    Assert.AreNotEqual(source.Stream.GetString(), "");
}
@stephentoub
Copy link
Member

stephentoub commented May 1, 2017

What's the question? You asked the same question on stackoverflow and got this answer:
http://stackoverflow.com/questions/43573321/httpclient-302-redirect/43576738#43576738
What's wrong with that answer?

@XaveScor
Copy link
Author

XaveScor commented May 1, 2017

No, this is not question. Why HttpClient don't redirect on specific url? I think HttpClient should redirect on any pages which have Location header.

@stephentoub
Copy link
Member

It's not based on the presence of a Location header: it's based on the response status code.

But that's not the issue here. The issue here is that you have an https URL that's trying to redirect to an http URL, which is explicitly blocked for security reasons.

cc: @davidsh, @bartonjs, @danmosemsft

@XaveScor
Copy link
Author

XaveScor commented May 1, 2017

How to disable this behavior? Have we some options for it? Security is unimportant thing in my problem.

@davidsh
Copy link
Contributor

davidsh commented May 1, 2017

If you need to handle HTTPS -> HTTP redirection, you should disable automatic redirection in HttpClient. Then handle the 3xx responses yourself.

var handler = new HttpClientHandler();
handler.AllowAutomaticRedirect = false;
var client = new HttpClient(handler);

@XaveScor
Copy link
Author

XaveScor commented May 2, 2017

Wow. Why we can't add the flag "enable redirect https -> http"?

@karelz
Copy link
Member

karelz commented May 4, 2017

We could. Is it worth it? The workaround above is reasonable.
Auto-redirecting is also kind of security risk, so not having it exposed as first-class citizen feels like the right thing to do ...

@davidsh
Copy link
Contributor

davidsh commented Jul 10, 2017

Closing as by-design. If you would like to raise this as a new design issue/discussion, please contribute to https://github.com/dotnet/designs/issues/9

@davidsh davidsh closed this as completed Jul 10, 2017
@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 2.1.0 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Net.Http question Answer questions and provide assistance, not an issue with source code or documentation.
Projects
None yet
Development

No branches or pull requests

5 participants