Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 如何配置HttpClient在收到301 HTTP状态代码时不自动重定向?_C# 4.0_Asp.net Web Api_Dotnet Httpclient - Fatal编程技术网

C# 4.0 如何配置HttpClient在收到301 HTTP状态代码时不自动重定向?

C# 4.0 如何配置HttpClient在收到301 HTTP状态代码时不自动重定向?,c#-4.0,asp.net-web-api,dotnet-httpclient,C# 4.0,Asp.net Web Api,Dotnet Httpclient,考虑重定向的ASP.NET Web API服务 public class ThisController : ApiController { /* more methods */ public override HttpResponseMessage Post() { var result = new HttpResponseMessage(HttpStatusCode.MovedPermanently); // Post requests

考虑重定向的ASP.NET Web API服务

public class ThisController : ApiController
{
    /* more methods */

    public override HttpResponseMessage Post()
    {
        var result = new HttpResponseMessage(HttpStatusCode.MovedPermanently);
        // Post requests should be made to "ThatController" instead.
        string uri = Url.Route("That", null);
        result.Headers.Location = new Uri(uri, UriKind.Relative);
        return result;
    }
}
为了验证将数据发布到“api/this”会将您重定向到“api/that”,我有以下测试方法:

[TestMethod]
public void PostRedirects()
{
    using (var client = CreateHttpClient("application/json"))
    {
        var content = CreateContent(expected, "application/json");
        using (var responseMessage = client.PostAsync("api/this", content).Result)
        {
            Assert.AreEqual(HttpStatusCode.MovedPermanently, responseMessage.StatusCode);
            Assert.AreEqual(new Uri("https://api.example.com/api/that"), responseMessage.Headers.Location);
        }
    }
}

protected HttpClient CreateHttpClient(string mediaType)
{
    var client = new HttpClient();
    client.BaseAddress = new Uri("http://api.example.com/");
    MediaTypeWithQualityHeaderValue headerValue = MediaTypeWithQualityHeaderValue.Parse(mediaType);
    client.DefaultRequestHeaders.Accept.Add(headerValue);
    client.DefaultRequestHeaders.AcceptEncoding.Add(StringWithQualityHeaderValue.Parse("gzip"));
    client.DefaultRequestHeaders.AcceptEncoding.Add(StringWithQualityHeaderValue.Parse("deflate"));
    client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(new ProductHeaderValue("MyProduct", "1.0")));
    client.MaxResponseContentBufferSize = 1024*1024*8;
    return client;
}

protected ObjectContent CreateContent(T model, string mediaType)
{
    var requestMessage = new HttpRequestMessage();
    MediaTypeFormatter mediaTypeFormatter = null;
    switch (mediaType)
    {
        case "application/json":
            mediaTypeFormatter = new JsonMediaTypeFormatter();
            break;

        case "application/xml":
        case "text/xml":
            mediaTypeFormatter = new XmlMediaTypeFormatter();
            break;

        default:
            Assert.Fail();
            break;
    }

    return requestMessage.CreateContent(
        model,
        new[] { mediaTypeFormatter },
        new FormatterSelector());
}
真正发生的事情是,HTTP状态代码被发送到具有正确位置头的客户端,然后HttpClient自动对该URI执行GET。结果,我的考试总是不及格

如何将HttpClient配置为在收到301时不自动重定向,以便验证服务器响应是否正确?

尝试:

var handler = new HttpClientHandler() 
{
    AllowAutoRedirect = false
};

HttpClient client = new HttpClient(handler);

可能的重复是否可以在每个请求的基础上执行此操作,而不需要两个单独的
HttpClient
实例(即一个允许重定向,另一个不允许重定向)?也许您需要这样做:
handler.maxAutomaticDirections=1
@Dai否,不幸的是,如果不重新设计轮子,这是不可能的。但是如果你不介意使用第三方图书馆的话。差不多十年后。。。如何为使用
TestServer.CreateClient()
创建的干净数据库设置此选项??