【问题标题】:How to create a local proxy to handle invalid headers in http response如何创建本地代理来处理 http 响应中的无效标头
【发布时间】:2021-07-28 15:40:15
【问题描述】:

我需要向网站发出一个简单的 GET 请求并解析响应。

我打算使用下面的简单代码

var httpClient =_httpClientFactory.CreateClient();
var initialRequest = await httpClient.GetAsync(_config.WebsiteUrl, cancellationToken);

但是,该站点制作不当,并在其响应中发送了无效标头,即“X=Frame-Options”,导致以下异常。

System.Net.Http.HttpRequestException: Received an invalid header name: 'X=Frame-Options'.

很遗憾,我无法控制该网站,而且这个问题不太可能很快得到解决。因此需要一种解决方法。

我的想法是创建一个本地“中间人”,可以这么说,它会在到达我的代码之前拦截从站点发送的响应,删除有问题的标头,然后传递响应。

我不熟悉 C# 中的网络,我想知道这个用例是否有任何好的库或现有示例。

我尝试过使用 Flurl 库,但不幸的是,它在实现中使用了 httpclient,所以仍然会抛出异常。

或者我是否遗漏了一些明显的东西,并且有某种方法可以禁用引发上述异常的标头验证?

【问题讨论】:

  • this 看起来有用吗?
  • 嗨@gunr2171!我确实看到了这个问题,并且确实尝试过,但问题是它仍然使用 HTTP 客户端类的 SendAsync 方法,导致抛出相同的异常。请参阅该线程中的 David Norvall 的回复。不过谢谢!

标签: c# http httpclient


【解决方案1】:

如果您使用网络平台或App.confing 文件的桌面,则必须在web.config 文件中将useUnsafeHeaderParsing 设置为true以允许接收不安全的请求

<configuration>
  //..............................
  <system.net>
    <settings>
      <httpWebRequest useUnsafeHeaderParsing="true"/>
    </settings>
  </system.net>
  //..............................
</configuration>

在代码后面添加以下方法

public static bool SetAllowUnsafeHeaderParsing(bool value)
{
    //Get the assembly that contains the internal class
    Assembly aNetAssembly = Assembly.GetAssembly(typeof(System.Net.Configuration.SettingsSection));
    if (aNetAssembly != null)
    {
        //Use the assembly in order to get the internal type for the internal class
        Type aSettingsType = aNetAssembly.GetType("System.Net.Configuration.SettingsSectionInternal");
        if (aSettingsType != null)
        {
           //Use the internal static property to get an instance of the internal settings class.
           //If the static instance isn't created allready the property will create it for us.
           object anInstance = aSettingsType.InvokeMember("Section", BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.NonPublic, null, null, new object[] { });

           if (anInstance != null)
           {
              //Locate the private bool field that tells the framework is unsafe header parsing should be allowed or not
             FieldInfo aUseUnsafeHeaderParsing = aSettingsType.GetField("useUnsafeHeaderParsing", BindingFlags.NonPublic | BindingFlags.Instance);
             if (aUseUnsafeHeaderParsing != null)
             {
                aUseUnsafeHeaderParsing.SetValue(anInstance, value);
                return true;
             }
           }
        }
    }
    return false;
}

现在使用

SetAllowUnsafeHeaderParsing(true);
var httpClient =_httpClientFactory.CreateClient();
var initialRequest = await httpClient.GetAsync(_config.WebsiteUrl, cancellationToken);
var response = await initialRequest.Content.ReadAsStringAsync();

【讨论】:

  • 嗨!我在另一篇文章中看到了这一点,但这似乎是针对 ASP.NET 项目的,对吗?我的项目是一个 azure 函数,所以我认为这不会起作用,因为 System.Net.Configuration.SettingsSection 类在 .NET Core 上不可用
  • 我没有使用.NET Core,但我认为配置是一样的。
猜你喜欢
  • 2013-06-02
  • 1970-01-01
  • 2013-09-15
  • 2021-09-12
  • 2017-02-25
  • 2022-10-15
  • 2019-06-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多