查询Exchange Web服务失败,出现400错误请求

时间:2014-03-26 19:49:29

标签: powershell exchangewebservices

我正在尝试使用EWS和Powershell从特定日历中获取一些信息。 我有一个用于查询EWS的powershell函数

    Function EWSSoapRequest
{
    param (
        $soapRequest,
        $exchangeServerAddress

    )
    # Create the request
    $webRequest = [System.Net.WebRequest]::Create($exchangeServerAddress)
    $webRequest.ContentType = "text/xml"
    $webRequest.Headers.Add("Translate", "F")
    $webRequest.Method = "Post"
    $webRequest.Credentials = $credentials

    # Setup the soap request to send to the server
    $content = [System.Text.Encoding]::UTF8.GetBytes($soapRequest)
    $webRequest.ContentLength = $content.Length
    $requestStream = $webRequest.GetRequestStream()
    $requestStream.Write($content, 0, $content.Length)
    $requestStream.Close()

    # Get the xml response from the server
    $webResponse = $webRequest.GetResponse()
    $responseStream = $webResponse.GetResponseStream()
    $responseXml = [xml](new-object System.IO.StreamReader $responseStream).ReadToEnd()
    $responseStream.Close()
    $webResponse.Close()
    $responseXml

}

我可以使用此XML获取日历项列表:

$soapRequestOverview = @'
<?xml version="1.0" encoding="utf-8"?>
  <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Header>
      <t:RequestServerVersion Version="Exchange2010_SP2" />
      <t:ExchangeImpersonation>
        <t:ConnectingSID>
          <t:SmtpAddress>user@domain.dk</t:SmtpAddress>
        </t:ConnectingSID>
      </t:ExchangeImpersonation>
    </soap:Header>
  <soap:Body>
    <FindItem Traversal="Shallow" xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">
      <ItemShape>
        <t:BaseShape>IdOnly</t:BaseShape>
        <t:AdditionalProperties>
          <t:FieldURI FieldURI="calendar:Start"/>
          <t:FieldURI FieldURI="calendar:End"/>
          <t:FieldURI FieldURI="item:Subject"/>
        </t:AdditionalProperties>
      </ItemShape>
      <CalendarView MaxEntriesReturned="100" StartDate="{0}" EndDate="{1}"/>
      <ParentFolderIds>
        <t:DistinguishedFolderId Id="calendar"/>
      </ParentFolderIds>
    </FindItem>
  </soap:Body>
</soap:Envelope>
'@

这给了我一个给定日历中的条目列表(我在调用Web服务函数之前替换了状态和结束日期。然后从返回的日历项列表中我使用ItemID属性来尝试查询详细信息,如位置但是我在这里遇到了问题。

我使用这个XML进行查询并像以前一样使用EWSSoapRequest函数,但我一直收到400 Bad请求。如果我将xml复制/粘贴到SoapUI中,那么我会得到一个与预期数据一致的响应。

    $SoapRequestDetail = @'
  <?xml version="1.0" encoding="utf-8"?>
  <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Header>
      <t:RequestServerVersion Version="Exchange2010_SP2" />
      <t:ExchangeImpersonation>
        <t:ConnectingSID>
          <t:SmtpAddress>user@domain.dk</t:SmtpAddress>
        </t:ConnectingSID>
      </t:ExchangeImpersonation>
    </soap:Header>
    <soap:Body>
      <m:GetItem>
        <m:ItemShape>
          <t:BaseShape>AllProperties</t:BaseShape>
        </m:ItemShape>
        <m:ItemIds>
          <t:ItemId Id="AAMkADU3MzNlNjQxLTA3NDYtNDA4MS1hMmJhLTg5MmUxOTM2NzI3YwBGAAAAAABifraNBbAgRq+5NEGUOyNbBwBOxvhv/eH/R6wHrO/Hc4RhAAABnAHkAAChTN/6w47WS4YxX5iYDfnHAABfYm2zAAA=" />
        </m:ItemIds>
      </m:GetItem>
    </soap:Body>
  </soap:Envelope>
'@

只是我忽略了一些明显的东西吗?

1 个答案:

答案 0 :(得分:1)

尝试使用此XML

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
   <soap:Header>
     <t:RequestServerVersion Version="Exchange2010_SP2" />
     <t:ExchangeImpersonation>
       <t:ConnectingSID>
         <t:SmtpAddress>user@domain.com</t:SmtpAddress>
       </t:ConnectingSID>
     </t:ExchangeImpersonation>
</soap:Header>
  <soap:Body>
    <GetItem
      xmlns="http://schemas.microsoft.com/exchange/services/2006/messages"
      xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
      <ItemShape>
        <t:BaseShape>AllProperties</t:BaseShape>
      </ItemShape>
      <ItemIds>
        <t:ItemId Id="AAAlAF" />
      </ItemIds>
    </GetItem>
  </soap:Body>
</soap:Envelope>

干杯 格伦