如何访问响应头部值

huangapple go评论71阅读模式
英文:

How to access response header values

问题

我正在使用 .Net Core 7 进行 Web API 项目的开发。

在响应头中获取到了一些数值,因此我想将这些值存储在一个变量中。

请查看下面的截图,我想要获取

x-rate-limit-remaining 

的值。

英文:

I am working in web api project using .Net Core 7

I am getting values in Response headers.. So I want to access that values in one variable.

Please see below screenshot, I want to get value of

x-rate-limit-remaining

如何访问响应头部值

答案1

得分: 1

try
{
    HttpClient client = new();
    using var response = await client.GetAsync(URL);
    var HeaderKey = "x-rate-limit-remaining";
    if (response.Headers.Contains(HeaderKey))
    {
        var val = response.Headers.GetValues(HeaderKey).FirstOrDefault();
        Console.WriteLine(val);
    }
    else
    {
        Console.WriteLine($"the key '{HeaderKey}' not found in the headers!");
    }
}
catch (Exception ee)
{
    Console.Error.WriteLine(ee.Message);
}
英文:
try
{
	HttpClient client = new();
	using var response = await client.GetAsync(URL);
	var HeaderKey = "x-rate-limit-remaining";
	if (response.Headers.Contains(HeaderKey))
	{
		var val = response.Headers.GetValues(HeaderKey).FirstOrDefault();
		Console.WriteLine(val);
	}
	else
	{
		Console.WriteLine($"the key '{HeaderKey}' not found in the headers!");
	}
}
catch (Exception ee)
{
	Console.Error.WriteLine(ee.Message);
}

答案2

得分: 0

你可以使用 TryGetValuesStringValues

var headers = response.Headers;
string role = "";
if (headers.TryGetValues("Role", out Microsoft.Extensions.Primitives.StringValues headerValues))
{
    role = headerValues.FirstOrDefault();
}

或者使用 IEnumerable<string>

var headers = response.Headers;
string role = "";
if (headers.TryGetValues("Role", out IEnumerable<string> headerValues))
{
    role = headerValues.FirstOrDefault();
}
英文:

You can use TryGetValues with StringValues:

var headers = response.Headers;
string role = &quot;&quot;;
if (headers.TryGetValues(&quot;Role&quot;, out Microsoft.Extensions.Primitives.StringValues headerValues))
{
    role = headerValues.FirstOrDefault();
}

Or use IEnumerable<string>:

var headers = response.Headers;
string role = &quot;&quot;;
if (headers.TryGetValues(&quot;Role&quot;, out IEnumerable&lt;string&gt; headerValues))
{
    role = headerValues.FirstOrDefault();
}

答案3

得分: 0

根据你的以前的案例

你可以尝试以下方法:

if (HttpContext.Response.Headers.ContainsKey("x-rate-limit-remaining"))
{
    var header = HttpContext.Response.Headers["x-rate-limit-remaining"];
}

例如,在中间件中:

app.Use(async (context, next) =>
{
    if (context.Response.Headers.ContainsKey("x-rate-limit-remaining"))
    {
        var header = context.Response.Headers["x-rate-limit-remaining"];
    }
    
    await next.Invoke();
});
英文:

Based on your former case

You could try as below:

if (HttpContext.Response.Headers.ContainsKey(&quot;x-rate-limit-remaining&quot;))
    {
        var header = HttpContext.Response.Headers[&quot;x-rate-limit-remaining&quot;];
    }

For example in middleware :

app.Use(async (context, next) =&gt;
{

    
    if (context.Response.Headers.ContainsKey(&quot;x-rate-limit-remaining&quot;))
    {
        var header = context.Response.Headers[&quot;x-rate-limit-remaining&quot;];
    }
    
    await next.Invoke();
});

huangapple
  • 本文由 发表于 2023年5月29日 01:00:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352606.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定