错误处理System.Net.HttpWebRequest ::的GetResponse() [英] Error Handling in System.Net.HttpWebRequest::GetResponse()

查看:464
本文介绍了错误处理System.Net.HttpWebRequest ::的GetResponse()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用PowerShell脚本 System.Net.HttpWebRequest 与远程主机进行通信。

I have a Powershell script that uses System.Net.HttpWebRequest to communicate with a remote host.

我创建的要求,相应地设置属性并调用的GetResponse() getresponsestream()阅读整响应从服务器到的字符串。这工作得很好,只要服务器与200 OK消息进行响应。

I create the request, set properties accordingly and call getresponse() and getresponsestream() to read the entire response from the server to a string. This works fine as long as the server responds with a "200 OK" message.

如果服务器有一个400错误的请求或任何其他错误code响应,的GetResponse() getresponsestream()抛出异常和返回任何结果。我的问题是没有包括在我需要的,所以我可以做我自己的错误处理响应头更详细的错误信息。

If the server responds with a "400 Bad Request" or any other error code, getresponse() and getresponsestream() throw exceptions and return nothing. My problem is there is more detailed error information included in the response header which I need so I can do my own error handling.

如何将能够检索此400错误的请求信号?

How would I be able to retrieve this 400 Bad Request signal?

推荐答案

修改:我误解了这个问题,在第一,但事实证明,你可以使用<$ C检索响应头$ C> HttpWebResponse.GetResponseHeader()方法。如果出现异常, HttpWebRequest.GetResponse()方法的返回值 $空,你必须使用code检索HttpWebResponse对象,这样就可以调用 GetResponseHeader()就可以了:

Edit: I misunderstood the question at first, but it turns out that you can retrieve the response header by using the HttpWebResponse.GetResponseHeader() method. If an exception occurs, the HttpWebRequest.GetResponse() method returns $null, and you have to use this code to retrieve the HttpWebResponse object, so that you can call GetResponseHeader() on it:

# If an exception occurs, get the HttpWebResponse object from the WebException object
$HttpWebResponse = $Error[0].Exception.InnerException.Response;


我是pretty的相信你一定要坚持使用 System.Net.HttpWebRequest 不是 System.Net的。 Web客户端对象。下面是一个例子,类似你可能已经拥有:


I'm pretty sure you'll want to stick with the System.Net.HttpWebRequest instead of the System.Net.WebClient object. Here is an example, similar to what you probably already have:

# Create a HttpWebRequest using the Create() static method
$HttpWebRequest = [System.Net.HttpWebRequest]::Create("http://www.google.com/");

# Get an HttpWebResponse object
$HttpWebResponse = $HttpWebRequest.GetResponse();

# Get the integer value of the HttpStatusCode enumeration
Write-Host -Object $HttpWebResponse.StatusCode.value__;

本的GetResponse()方法返回一个 HttpWebResponse 对象,其中有一个名为状态code 属性,它指向在的HTTPStatus code .NET枚举值。一旦你得到一个引用计数,我们使用了值__ 属性来获取与该返回枚举值相关联的整数。

The GetResponse() method returns a HttpWebResponse object, which has a property named StatusCode, which points to a value in the HttpStatusCode .NET enumeration. Once you get a reference to the enumeration, we use the value__ property to get the integer that is associated with the returned enum value.

如果你从的GetResponse()方法的空值,那么你需要阅读最新的错误信息在你抓{..}块。该 Exception.ErrorRecord 属性应该是最有帮助的。

If you get a null value from the GetResponse() method, then you'll want to read the most current error message in your catch {..} block. The Exception.ErrorRecord property should be the most helpful.

try {
  $HttpWebResponse = $null;
  $HttpWebRequest = [System.Net.HttpWebRequest]::Create("http://www.asdf.com/asdf");
  $HttpWebResponse = $HttpWebRequest.GetResponse();
  if ($HttpWebResponse) {
    Write-Host -Object $HttpWebResponse.StatusCode.value__;
    Write-Host -Object $HttpWebResponse.GetResponseHeader("X-Detailed-Error");
  }
}
catch {
  $ErrorMessage = $Error[0].Exception.ErrorRecord.Exception.Message;
  $Matched = ($ErrorMessage -match '[0-9]{3}')
  if ($Matched) {
    Write-Host -Object ('HTTP status code was {0} ({1})' -f $HttpStatusCode, $matches.0);
  }
  else {
    Write-Host -Object $ErrorMessage;
  }

  $HttpWebResponse = $Error[0].Exception.InnerException.Response;
  $HttpWebResponse.GetResponseHeader("X-Detailed-Error");
}

<一个href="http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx">http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx

<一个href="http://msdn.microsoft.com/en-us/library/system.net.httpstatus$c$c.aspx">http://msdn.microsoft.com/en-us/library/system.net.httpstatus$c$c.aspx

<一个href="http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.aspx">http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.aspx

这篇关于错误处理System.Net.HttpWebRequest ::的GetResponse()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆