Powershell Invoke-WebRequest为POST

时间:2017-07-01 07:48:24

标签: xml powershell

提前道歉,因为此处缺乏理解以及此问题之前已被问过的可能性。我认为通过转移到 Powershell v5.1 进行排序后,结果比我想象的要复杂得多,所以我会更新问题。

我们一直在 PS 4.0版上开发脚本,原因是我们的客户拥有PS v4.0标准的Windows 2012 R2服务器。但是,此脚本无法在v4.x上正常运行,如下所示

这是我的代码,其中包含正确形成的xml代码段

$NETXNUA = "https://gate.sonile.com/ModSoap";
$ContentType = "text/xml";

# tried this and it this doesn't do anything
# [System.Net.ServicePointManager]::Expect100Continue = $false

$RequestBody = @"
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/1999/XMLSchema">
    <SOAP-ENV:Body>
        <sendMessage
        xmlns="http://gate.sonile.com/ModSoap" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
        <application xsi:type="xsd:string">RuFus</application>
        <password xsi:type="xsd:string">PingPong</password>
        <content xsi:type="xsd:string">Tiddles</content>
        <class xsi:type="xsd:string">mt_bubbles</class>
        </sendMessage>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
"@

try {
    $what = Invoke-WebRequest -Uri $NETXNUA -Method Post -Body $RequestBody -ContentType $ContentType -UseBasicParsing;
    }
catch {
    If ($_.Exception.Response.StatusCode.value__) {
    $crap = ($_.Exception.Response.StatusCode.value__ ).ToString().Trim();
    Write-Output "crap $crap";
    }

    If  ($_.Exception.Response.StatusDescription) {
    $crapdescription = ($_.Exception.Response.StatusDescription).ToString().Trim();
    Write-Output "crapdesc $crapdescription";  
    }

    If  ($_.Exception.Message) {
    $crapMessage = ($_.Exception.Message).ToString().Trim();
    Write-Output "crapmsg $crapMessage";
    }

    If  ($_.ErrorDetails.Message) {
    $ResponseBody = ($_.ErrorDetails.Message).ToString().Trim();
    $ResponseBody = $ResponseBody -replace "\s+", " ";
    }

    Write-Output "default $ResponseBody";
}

当我在 Windows 8.1 Pro 和标准Powershell上运行此脚本时会发生这种情况

Name                           Value                                                                                                          
----                           -----                                                                                                          
PSVersion                      4.0                                                                                                            
WSManStackVersion              3.0                                                                                                            
SerializationVersion           1.1.0.1                                                                                                        
CLRVersion                     4.0.30319.42000                                                                                                
BuildVersion                   6.3.9600.17400                                                                                                 
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}                                                                                           
PSRemotingProtocolVersion      2.2              

下面是结果,请注意“$ _。Exception.Message”是唯一包含任何值的项目

crapmsg '"utf-8"' is not a supported encoding name. For information on 
defining a custom encoding, see the documentation for the Encoding.Regi
sterProvider method.
Parameter name: name

换句话说,一个悲惨的失败,除了信不信,主持人处理了后期数据

现在使用更新的v5.1 Powershell在 Windows Server 2008 R2 上运行完全相同的脚本

Name                           Value                                                                                                            
----                           -----                                                                                                            
PSVersion                      5.1.14409.1005                                                                                                   
PSEdition                      Desktop                                                                                                          
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                          
BuildVersion                   10.0.14409.1005                                                                                                  
CLRVersion                     4.0.30319.42000                                                                                                  
WSManStackVersion              3.0                                                                                                              
PSRemotingProtocolVersion      2.3                                                                                                              
SerializationVersion           1.1.0.1

以下是“$ _.Exception.Message”

的结果
crapmsg Value cannot be null.
Parameter name: name

哇,那不一样,这里发生了什么......?

然后,让我们使用Powershell v5.1更新在Windows Server 2012 R2 Azure VM 上再次尝试这个完全相同的脚本

Name                           Value                                                                                       
----                           -----                                                                                       
PSVersion                      5.1.14409.1005                                                                              
PSEdition                      Desktop                                                                                     
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                     
BuildVersion                   10.0.14409.1005                                                                             
CLRVersion                     4.0.30319.42000                                                                             
WSManStackVersion              3.0                                                                                         
PSRemotingProtocolVersion      2.3                                                                                         
SerializationVersion           1.1.0.1  

现在,与2008 R2服务器上的结果相同,只有“$ _.Exception.Message”返回任何内容。至少那是一致的,这是因为PS版本是相同的。

crapmsg Value cannot be null.
Parameter name: name

我使用标准安装的Powershell v5.1在Windows集合 Windows 10 Home Edition 的ragamuffin上尝试此脚本

Name                           Value                                                                                                                                                         
----                           -----                                                                                                                                                         
PSVersion                      5.1.14393.1358                                                                                                                                                
PSEdition                      Desktop                                                                                                                                                       
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                       
BuildVersion                   10.0.14393.1358                                                                                                                                               
CLRVersion                     4.0.30319.42000                                                                                                                                               
WSManStackVersion              3.0                                                                                                                                                           
PSRemotingProtocolVersion      2.3                                                                                                                                                           
SerializationVersion           1.1.0.1 
天啊,我不相信......奇迹!它可以完美地处理200美元的响应,以及我可以处理的xml中的主机响应。

StatusCode        : 200
StatusDescription : OK
Content           : <?xml version="1.0" encoding="UTF-8"?>
                    <tns1:Envelope 
xmlns:tns1="http://schemas.xmlsoap.org/soap/envelope/">...etc
RawContent        : HTTP/1.1 200 OK
                    Keep-Alive: timeout=15, max=100
                    Connection: Keep-Alive
                    Content-Length: 411
                    Content-Type: text/xml; charset="utf-8"
                    Date: Thu, 06 Jul 2017 04:02:15 GMT
                    Server: Apache
                    Via: 1.1 ga...
Forms             : 
Headers           : {[Keep-Alive, timeout=15, max=100], [Connection, Keep-
Alive], [Content...etc
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : 
RawContentLength  : 411

如果只有我的客户会抛弃他们喜欢的那些讨厌的Windows服务器而不是使用Windows 10,那么一切都会完全排序。!

突发新闻!刚试过 Windows Server 2016 Azure VM ,这也很好。

Name                           Value                                                                                               
----                           -----                                                                                               
PSVersion                      5.1.14393.479                                                                                       
PSEdition                      Desktop                                                                                             
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                             
BuildVersion                   10.0.14393.479                                                                                      
CLRVersion                     4.0.30319.42000                                                                                     
WSManStackVersion              3.0                                                                                                 
PSRemotingProtocolVersion      2.3                                                                                                 
SerializationVersion           1.1.0.1  

是否有一些秘密参数可以使这个脚本在各种平台和Powershell版本上正常工作......?

我使用Invoke-WebRequest创建的每个其他脚本在PS v4&amp; v5就是这个引起问题的那个。

任何提示都非常感激

以前我尝试使用xml的文件输入并使用-InFile读取...

Invoke-WebRequest "https://gateway.mobil.com/MSoap" -Method Post -ContentType "text/xml" -InFile "C:\Scripts\mobil.xml" -OutFile "C:\PWScripts\mobil_out.xml"

这很好,但它不适合我的应用程序

1 个答案:

答案 0 :(得分:0)

整个星期都花了不少时间辛苦工作......奇怪的是,它只是在成功的200状态返回时被轰炸,错误被退回就好了!

原来,Invoke-WebRequest有一个难以理解的问题,由@vane在2016年4月发布Why is Invoke-WebRequest and Invoke-RestMethod failing and succeeding at the same time?

因此,如果我错了,请纠正我,唯一已知的解决方案是恢复到标准的http WebRequest调用(包含你自己的错误处理);

    $encodedContent = [System.Text.Encoding]::UTF8.GetBytes($RequestBody)

    $webRequest = [System.Net.WebRequest]::Create($NETXNUA)

    $webRequest.Method = "Post"
    $webRequest.ContentType = $ContentType
    $webRequest.ContentLength = $encodedContent.length

    $requestStream = $webRequest.GetRequestStream()
    $requestStream.Write($encodedContent, 0, $encodedContent.length)
    $requestStream.Close()

    [System.Net.WebResponse] $response = $webRequest.GetResponse();

我已经验证了这种旧式机制在以下平台上运行良好;

Win 8.1 Pro with PS v4.0
Server 2008 R2 with PS 5.1
Server 2012 R2 with PS 5.1 (Azure VM)
Server 2012 R2 with PS v4.0 (Azure VM)
Win 10 Home with PS 5.1 
Server 2016 with PS v5.1 (Azure VM)

我承认Invoke-WebRequest脚本在Win 10和Server 2016上运行完美,所以最终问题就会消失!

相关问题