gpt4 book ai didi

c# - HttpWebResponse StatusCode toString

转载 作者:行者123 更新时间:2023-11-30 20:49:33 26 4
gpt4 key购买 nike

我正在尝试处理 302 重定向响应。我看到 HttpStatusCode 的 toString 方法有不同的行为。

HttpWebResponse response = (HttpWebResponse) _webRequest.GetResponse();
if (response.StatusCode != HttpStatusCode.OK)
throw new TestException(TestException.HttpErrorClass,response.StatusCode.ToString(),"");

问题是当我调试并查看响应对象时,StatusCode 显示“FOUND”状态。但是当我对这段代码执行 toString 操作时,我得到了“重定向”字符串。此外,它不会始终返回“Redirect”字符串,它有时会在我同事的机器上返回“FOUND”字符串。

从 .net 4.0 到 .net 4.5,toString 方法有什么变化吗?有人见过类似的问题吗?

最佳答案

如果查看 HttpStatusCode 的定义,您会发现值 302 有多个枚举成员。根据文档,当存在多个定义时,生成的字符串值将不会始终相同。看一下 GetName 方法(ToString 几乎就是这样做的)。

http://msdn.microsoft.com/en-us/library/system.enum.getname(v=vs.110).aspx

If multiple enumeration members have the same underlying value, the GetName method guarantees that it will return the name of one of those enumeration members. However, it does not guarantee that it will always return the name of the same enumeration member. As a result, when multiple enumeration members have the same value, your application code should never depend on the method returning a particular member's name.

查看此答案以获得更多见解:Why is it okay for an enum to have two different names with the same numeric value?

关于c# - HttpWebResponse StatusCode toString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23481972/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com