2
我有2个网址:https://pcr.apple.com/id868222886
和https://jigsaw.w3.org/HTTP/300/302.html
。两者都有位置链接和302响应代码。HttpClient 302重定向
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(), "");
}
为什么?有什么区别?
区别在哪里? 302 =找到(通常用于重定向),301 =永久移动。见[这里](https://developer.att.com/application-resource-optimizer/docs/best-practices/http-300-status-codes)。 – john
@john我有两个相同的,我认为,链接。但是HttpClient在这个链接上的工作是不同的。我的问题是为什么。为什么行为不同? – XaveScor