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