简体   繁体   English

HttpClient在ASP.NET Core上的行为有所不同

[英]HttpClient behaving differently on ASP.NET Core

My issue lies within the PostAsync action of the HttpClient . 我的问题在于HttpClientPostAsync操作中。 A code excerpt used in my C# console app behaves totally different than the same code used in ASP.NET Core. 我的C#控制台应用程序中使用的代码摘录的行为与ASP.NET Core中使用的相同代码的行为完全不同。

using (var httpClient = new HttpClient())
{
    var parameters = new Dictionary<string, string> { { "various", "params" } };
    var encodedContent = new FormUrlEncodedContent(parameters);

    var response = await httpClient.PostAsync(url, encodedContent).ConfigureAwait(false);
    if (response.StatusCode == HttpStatusCode.OK)
    {
        var responseContent = await response.Content.ReadAsStringAsync ().ConfigureAwait (false);
        return responseContent;
    }
}

Calling the method (GetData contains the upper code): 调用方法(GetData包含上面的代码):

var unprocessedString = GetData("http://www.whatever.com/info.html").Result;

In the unprocessedString I end up with a valid html string unprocessedString我最终得到一个有效的html字符串

The exact same code applied in my ASP .NET Core returns a completely different result. 我的ASP .NET Core中使用的代码完全相同,返回的结果完全不同。

The returned information looks like a huge JS script, I did check the fiddler request headers and the only difference is the Accept-Encoding: gzip, deflate header being created by the ASP call, I did remove it and the results stays the same. 返回的信息看起来像一个巨大的JS脚本,我检查了提琴手请求标头,唯一的区别是Accept-Encoding: gzip, deflate由ASP调用创建的Accept-Encoding: gzip, deflate标头,我确实删除了它,结果保持不变。

<html><head><meta http-equiv="Pragma" content="no-cache"/>
<script>
a huge javascript function
<noscript>Please enable JavaScript to view the page content.</noscript>

I did manage to find the issue. 我确实设法找到了问题。 My http - post initially gets to the upper mentioned JavaScript that redirects my call to the seeked content ( This is what was happening when running the code from a .NET console app). 我的http帖子最初到达上面提到的JavaScript,它将我的调用redirects到所需的内容(这是从.NET控制台应用程序运行代码时发生的情况)。 As far as I can see the ASP .NET Core implementation of the HttpClient differs in behavior because the exactly same code (and inquired resource) behaved totally different because the redirect never happened, thus the upper JS code ending up when I was reading info from that post. 据我所见, HttpClient的ASP .NET Core实现的行为有所不同,因为完全相同的代码(和查询的资源)的行为完全不同,因为redirect从未发生,因此,当我从中读取信息时,上层JS代码结束了该职位。

This is how I've managed to fix it. 这就是我设法解决的方法。

Firstly I declared an HttpClientHandler with the AllowAutoRedirect property to true . 首先,我声明了一个AllowAutoRedirect属性为trueHttpClientHandler

 HttpClientHandler httpClientHandler = new HttpClientHandler { AllowAutoRedirect = true };

Then I just added this object to HttpClient 's constructor 然后我只是将此对象添加到HttpClient的构造函数中

 HttpClient = new HttpClient(httpClientHandler);

Edit 1 编辑1

I did manage to see a difference in the System.Net.Http versions. 我确实设法看到System.Net.Http版本中的差异。 In the console app I have 4.0.0.0 In the .NET Core I have 4.1.1.1 在控制台应用程序中,我有4.0.0.0 。在.NET Core中,我有4.1.1.1

Probably the latter version doesn't have the default behavior 4.0.0.0 had. 后者可能没有默认行为4.0.0.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM