2012-05-17 16 views
31

consideri un servizio Web API ASP.NET che reindirizzaCome si configura HttpClient per non reindirizzare automaticamente quando riceve un codice di stato HTTP 301?

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; 
    } 
} 

Cercando di verificare che i dati POST'ing di "api/questo" vi favore usare "api/che", ho il seguente metodo:

[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()); 
} 

ciò che realmente accade cioè che un codice di stato HTTP viene inviato al cliente con l'intestazione posizione corretta e che HttpClient poi esegue automaticamente un GET su quel URI. Di conseguenza, il mio test non passa mai.

Come configurare HttpClient in modo che non venga reindirizzato automaticamente quando riceve un 301 in modo da poter verificare la risposta del server?

+0

Eventuali duplicati di [? Come posso ottenere System.Net.Http.HttpClient di non seguire 302 redirect] (http://stackoverflow.com/questions/10453892/how-can -i-get-system-net-http-HttpClient-a-non-follow-302-redirect) –

risposta

67

Prova:

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

HttpClient client = new HttpClient(handler); 
Problemi correlati