我怎样才能得到System

我怎样才能得到System

本文介绍了我怎样才能得到System.Net.Http.HttpClient不遵循302重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从的NuGet 使用。

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 aWebRequestHandler argument. The HttpClient class uses thisWebRequestHandler for sending requests.

WebRequestHandler 类提供了一个名为 AllowAutoRedirect
配置重定向行为属性。此属性设置为false
指示的HttpClient 来不遵循重定向响应。

The WebRequestHandler class provides a property called AllowAutoRedirectto configure the redirect behaviour. Setting this property to falseinstructs 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重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 05:27