.net 如何在 PowerShell 中获取数字 HTTP 状态代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1473358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to obtain numeric HTTP status codes in PowerShell
提问by halr9000
I know of a few good waysto build web clients in PowerShell: the .NET classes System.Net.WebClient and System.Net.HttpWebRequest, or the COM object Msxml2.XMLHTTP. From what I can tell, the only one which gives you access to the numeric status code (e.g. 200, 404), is the last, the COM object. The problem I have is that I don't like the way it works and I don't like relying on the COM object being there. I also know that from time to time Microsoft will decide to kill COM objects (ActiveX kill bits) due to security vulnerabilities and so on.
我知道一些在 PowerShell 中构建 Web 客户端的好方法:.NET 类 System.Net.WebClient 和 System.Net.HttpWebRequest,或 COM 对象 Msxml2.XMLHTTP。据我所知,唯一能让您访问数字状态代码(例如 200、404)的是最后一个 COM 对象。我的问题是我不喜欢它的工作方式,也不喜欢依赖存在的 COM 对象。我也知道微软有时会因为安全漏洞等原因决定杀死COM对象(ActiveX kill bits)。
Is there another .NET method I'm missing? Is the status code in one of these other two objects and I just don't know how to get at it?
我还缺少另一种 .NET 方法吗?状态代码是否在另外两个对象之一中,我只是不知道如何获取它?
回答by halr9000
Using both x0n and joshua ewer's answers to come full circle with a code example, I hope that's not too bad form:
使用 x0n 和 joshua ewer 的答案来完整循环代码示例,我希望这不是太糟糕的形式:
$url = 'http://google.com'
$req = [system.Net.WebRequest]::Create($url)
try {
$res = $req.GetResponse()
}
catch [System.Net.WebException] {
$res = $_.Exception.Response
}
$res.StatusCode
#OK
[int]$res.StatusCode
#200
回答by x0n
Use the [system.net.httpstatuscode]enumerated type.
使用[system.net.httpstatuscode]枚举类型。
ps> [enum]::getnames([system.net.httpstatuscode])
Continue
SwitchingProtocols
OK
Created
Accepted
NonAuthoritativeInformation
NoContent
ResetContent
...
To get the numeric code, cast to [int]:
要获取数字代码,请转换为 [int]:
ps> [int][system.net.httpstatuscode]::ok
200
Hope this helps,
希望这可以帮助,
-Oisin
-Oisin
回答by joshua.ewer
I realize the question's title is about powershell, but not really what the question is asking? Either way...
我意识到问题的标题是关于 powershell,但并不是真正的问题在问什么?无论哪种方式...
The WebClient is a very dumbed down wrapper for HttpWebRequest. WebClient is great if you just doing very simple consumption of services or posting a bit of Xml, but the tradeoff is that it's not as flexible as you might want it to be. You won't be able to get the information you are looking for from WebClient.
WebClient 是一个非常笨拙的 HttpWebRequest 包装器。如果您只是执行非常简单的服务消费或发布一些 Xml,WebClient 非常棒,但代价是它并不像您希望的那样灵活。您将无法从 WebClient 获得您正在寻找的信息。
If you need the status code, get it from the HttpWebResponse. If you were doing something like this (just posting a string to a Url) w/ WebClient:
如果您需要状态代码,请从 HttpWebResponse 中获取。如果您正在使用 WebClient 执行这样的操作(只需将字符串发布到 Url):
var bytes =
System.Text.Encoding.ASCII.GetBytes("my xml");
var response =
new WebClient().UploadData("http://webservice.com", "POST", bytes);
then you'd do this with HttpWebRequest to get status code. Same idea, just more options (and therefore more code).
然后你会用 HttpWebRequest 来获取状态代码。同样的想法,只是更多的选择(因此更多的代码)。
//create a stream from whatever you want to post here
var bytes =
System.Text.Encoding.ASCII.GetBytes("my xml");
var request =
(HttpWebRequest)WebRequest.Create("http://webservice.com");
//set up your request options here (method, mime-type, length)
//write something to the request stream
var requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
var response = (HttpWebResponse)request.GetResponse();
//returns back the HttpStatusCode enumeration
var httpStatusCode = response.StatusCode;
回答by CommonToast
It looksvery easy
它看起来很容易
$wc = New-Object NET.WebClient
$wc.DownloadString($url)
$wc.ResponseHeaders.Item("status")
You can find the other available Response Headers in the ResponseHeaders property (like content-type, content-length, x-powered-by and such), and retrieve any of them via the Item() method.
您可以在 ResponseHeaders 属性中找到其他可用的响应标头(如 content-type、content-length、x-powered-by 等),并通过 Item() 方法检索它们中的任何一个。
... but as rob mentions below, sadly, the status property is not available here
...但正如 rob 在下面提到的,遗憾的是,status 属性在这里不可用

