我怎样才能得到System.Net.Http.HttpClient不遵循302重定向? [英] How can I get System.Net.Http.HttpClient to not follow 302 redirects?
问题描述
从的NuGet 使用 HttpClient的。
Using HttpClient from NuGet.
该应用程序发送带有client.PostAsync后()。我想它不遵循302重定向。
The app sends a post with client.PostAsync(). I'd like it to NOT follow 302 redirects.
怎么样?
我想我可以只设置 AllowAutoRedirect
中的这个答案。
I figure I can just set AllowAutoRedirect
as described in this answer.
但我怎么让的HttpWebRequest
一个PostAsync内使用()调用?
But how do I get the HttpWebRequest
used within a PostAsync() call?
推荐答案
其中一个重载的的HttpClient
构造函数将
WebRequestHandler
参数。在的HttpClient
类使用这个
WebRequestHandler
发送请求。
One of the overloads of the HttpClient
constructor takes a
WebRequestHandler
argument. The HttpClient
class uses this
WebRequestHandler
for sending requests.
WebRequestHandler
类提供了一个名为 AllowAutoRedirect
配置重定向行为属性。此属性设置为false
指示的HttpClient
来不遵循重定向响应。
The WebRequestHandler
class provides a property called AllowAutoRedirect
to configure the redirect behaviour. Setting this property to false
instructs the HttpClient
to not follow redirect responses.
下面是一个小代码示例:
Here is a small code sample:
WebRequestHandler webRequestHandler = new WebRequestHandler();
webRequestHandler.AllowAutoRedirect = false;
HttpClient httpClient = new HttpClient(webRequestHandler);
// Send a request using GetAsync or PostAsync
Task<HttpResponseMessage> response = httpClient.GetAsync("http://www.google.com");
这篇关于我怎样才能得到System.Net.Http.HttpClient不遵循302重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!