Powershell网络请求 - 400错误请求

时间:2012-11-29 06:12:00

标签: rest powershell http-status-code-400 system.net.webexception

我正在构建一个powershell脚本来在类似REST的服务器上执行命令,我正在使用powershell将XML数据发送到Web服务。除非“应用程序”产生错误,否则一切都工作正常。该应用程序回复xml中的详细信息,并给我一个Web请求错误(在这种情况下为400),这是我遇到问题。每当我得到一个不是200的Web请求错误时,命令就会抛出一个错误,这会搞砸我处理错误的方式。

这是我接收的响应示例(使用fiddler):

HTTP/1.1 400 Bad Request
Date: Thu, 29 Nov 2012 04:29:48 GMT
Content-Type: text/xml;charset=ISO-8859-1
Connection: close

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Status>
    <operation>Add Network</operation>
    <result>ERROR</result>
    <resultCode>REASON_210</resultCode>
</Status>

这是我在powershell中收到的错误:

Exception calling "UploadData" with "3" argument(s): "The remote server returned an error: (400) Bad Request."
At C:\Scripting\test.ps1:47 char:34
+     $response = $query.UploadData <<<< ($url,"POST",$byteArray)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

Exception calling "GetString" with "1" argument(s): "Array cannot be null.
Parameter name: bytes"
At C:\Scripting\test.ps1:48 char:62
+     $stringresponse = [System.Text.Encoding]::ASCII.GetString <<<< ($response)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

我进一步挖掘了错误,这就像我可以检索响应内容(XML)一样接近。

我以为我可能会把它拉出来......

$error[1].exception.InnerException.Data

但它是空的:(

PS C:\Scripting> $error[1].exception.InnerException | fl -force


Status         : ProtocolError
Response       : System.Net.HttpWebResponse
Message        : The remote server returned an error: (400) Bad Request.
Data           : {}
InnerException :
TargetSite     : Byte[] UploadDataInternal(System.Uri, System.String, Byte[], System.Net.WebRequest ByRef)
StackTrace     :    at System.Net.WebClient.UploadDataInternal(Uri address, String method, Byte[] data, WebRequest& request)
                    at System.Net.WebClient.UploadData(Uri address, String method, Byte[] data)
                    at System.Net.WebClient.UploadData(String address, String method, Byte[] data)
                    at UploadData(Object , Object[] )
                    at System.Management.Automation.MethodInformation.Invoke(Object target, Object[] arguments)
                    at System.Management.Automation.DotNetAdapter.AuxiliaryMethodInvoke(Object target, Object[] arguments, MethodInformation methodInformation, Object[]
                 originalArguments)
HelpLink       :
Source         : System


PS C:\Scripting> $error[1].exception.InnerException.response | fl


IsMutuallyAuthenticated : False
Cookies                 : {}
Headers                 : {Connection, Transfer-Encoding, Content-Type, Date}
ContentLength           : -1
ContentEncoding         :
ContentType             : text/xml;charset=ISO-8859-1
CharacterSet            : ISO-8859-1
Server                  :
LastModified            : 29/11/12 4:04:19 PM
StatusCode              : BadRequest
StatusDescription       : Bad Request
ProtocolVersion         : 1.1
ResponseUri             : https://api-au.dimensiondata.com/oec/0.9/cdefb891-1552-4008-8638-7393eda8d7e1/network
Method                  : POST
IsFromCache             : False

这是我正在使用的powershell代码:

function send_web ($url,$data) {
    [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
    $query = new-object system.net.WebClient
    $auth = 'Basic ' + [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($username+":"+$password ))
    $query.Headers.Add('Authorization', $auth )
    [byte[]]$byteArray = [System.Text.Encoding]::ASCII.GetBytes($data) 
    $query.Headers.Add("ContentType","application/xml")
    $response = $query.UploadData($url,"POST",$byteArray)
    $stringresponse = [System.Text.Encoding]::ASCII.GetString($response)
    return $stringresponse
}

这似乎不应该是一件很难的事情,任何帮助都会非常感激:)

理查德

0 个答案:

没有答案