title: powershell soaprequest with username and password
body: <p>I need to make a Soap Request from Powershell, I am on Powershell 2.0, I can find lots of examples but I also need to include a username and password to get through the api.</p>
  
  <p>I get a response of
  Exception calling "GetResponse" with "0" argument(s): "The underlying connectio
  n was closed: An unexpected error occurred on a send."</p>
  
  <p>My Powershell code (I have redacted some sensitive stuff) is  </p>
  
  <pre><code>function Execute-SOAPRequest 
  ( 
          [Xml]    $SOAPRequest, 
          [String] $URL 
  ) 
  { 
          write-host "Sending SOAP Request To Server: $URL" 
          $soapWebRequest = [System.Net.WebRequest]::Create($URL) 
          $soapWebRequest.Headers.Add("SOAPAction","`"`"")
  
          $soapWebRequest.ContentType = "text/xml;charset=`"utf-8`"" 
          $soapWebRequest.Accept      = "text/xml" 
          $soapWebRequest.Method      = "POST" 
  
          $username = "USERNAME"
          $password = "Password" | ConvertTo-SecureString -asPlainText -Force
          $cred = New-Object System.Management.Automation.PSCredential($username,$password)
          $soapWebRequest.Credentials = $cred
          #$soapWebRequest.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
          #$soapWebRequest.UseDefaultCredentials = $true           
  
          write-host $soapWebRequest
  
          write-host "Initiating Send."
          $requestStream = $soapWebRequest.GetRequestStream() 
          write-host $requestStream 
          $SOAPRequest.Save($requestStream)  
          $requestStream.Close() 
  
          write-host "Send Complete, Waiting For Response." 
          $resp = $soapWebRequest.GetResponse() 
          $responseStream = $resp.GetResponseStream() 
          $soapReader = [System.IO.StreamReader]($responseStream) 
          $ReturnXml = [Xml] $soapReader.ReadToEnd() 
          $responseStream.Close() 
  
          write-host "Response Received."
  
          return $ReturnXml 
  }
  
  $url = 'https://zkncsavia049.via.novonet:7180/ws/activeservices/pmspapi_nomination'
  $soap = [xml]@'
  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;soapenv:Envelope xmlns:pms="http://www.mydomain.co.uk/pmsapi_nomination" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
                       xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wssu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"&gt;
     &lt;soapenv:Header&gt;&lt;wsse:Security&gt;&lt;wsse:UsernameToken wssu:Id="UsernameToken-4B81F4838BB6D8A60715299310629901"&gt;&lt;wsse:Username&gt;igyadmin&lt;/wsse:Username&gt;
               &lt;wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"&gt;V3rm0nt&lt;/wsse:Password&gt;
               &lt;wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"&gt;x9DSipKzGHLbvyCOXKIY6A==&lt;/wsse:Nonce&gt;
               &lt;wssu:Created&gt;2018-06-25T12:51:02.986Z&lt;/wssu:Created&gt;&lt;/wsse:UsernameToken&gt;&lt;/wsse:Security&gt;     
     &lt;/soapenv:Header&gt;
     &lt;soapenv:Body&gt;
        &lt;pms:pmsapi_nomination.getlist.ApiRequest xmlns:ns2="http://www.mydomain.co.uk/pmsapi_nomination"&gt;
           &lt;ApiRequest&gt;
              &lt;LogonInfo&gt;
                 &lt;BusinessGroup&gt;GMP&lt;/BusinessGroup&gt;
                 &lt;AdminGroup&gt;ADMN&lt;/AdminGroup&gt;
              &lt;/LogonInfo&gt;
              &lt;BUSINESS_GROUP&gt;
                 &lt;value&gt;GMP&lt;/value&gt;
              &lt;/BUSINESS_GROUP&gt;
              &lt;CONTEXT&gt;
                 &lt;value&gt;F&lt;/value&gt;
              &lt;/CONTEXT&gt;
              &lt;REFNO&gt;
                 &lt;value&gt;0039760&lt;/value&gt;
              &lt;/REFNO&gt;
           &lt;/ApiRequest&gt;
        &lt;/pms:pmsapi_nomination.getlist.ApiRequest&gt;
     &lt;/soapenv:Body&gt;
  &lt;/soapenv:Envelope&gt;
  '@
  
  $ret = Execute-SOAPRequest $soap $url 
  </code></pre>
  
  <p>I can get the call to work through SOAPUI the raw XML is </p>
  
  <pre><code>POST http://zkncsavia049.via.novonet:7180/ws/activeservices/pmsapi_nomination/HTTP/1.1
  Accept-Encoding: gzip,deflate
  Content-Type: text/xml;charset=UTF-8
  SOAPAction: ""
  Content-Length: 1541
  Host: zkncsavia049.via.novonet:7180
  Connection: Keep-Alive
  User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
  
  &lt;soapenv:Envelope xmlns:pms="http://www.mydomain.co.uk/pmsapi_nomination" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wssu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"&gt;
     &lt;soapenv:Header&gt;&lt;wsse:Security&gt;&lt;wsse:UsernameToken wssu:Id="UsernameToken-78C15367B2B1287BB3153000735070610"&gt;&lt;wsse:Username&gt;USERNAME&lt;/wsse:Username&gt;&lt;wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"&gt;password&lt;/wsse:Password&gt;&lt;wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"&gt;q5B6XRmHsecmHouCqH03qA==&lt;/wsse:Nonce&gt;&lt;wssu:Created&gt;2018-06-26T10:02:30.696Z&lt;/wssu:Created&gt;&lt;/wsse:UsernameToken&gt;&lt;/wsse:Security&gt;     
     &lt;/soapenv:Header&gt;
     &lt;soapenv:Body&gt;
        &lt;pms:pmsapi_nomination.getlist.ApiRequest xmlns:ns2="http://www.aquilauk.co.uk/pmsapi_nomination"&gt;
           &lt;ApiRequest&gt;
              &lt;LogonInfo&gt;
                 &lt;BusinessGroup&gt;GMP&lt;/BusinessGroup&gt;
                 &lt;AdminGroup&gt;ADMN&lt;/AdminGroup&gt;
              &lt;/LogonInfo&gt;
              &lt;BUSINESS_GROUP&gt;
                 &lt;value&gt;GMP&lt;/value&gt;
              &lt;/BUSINESS_GROUP&gt;
              &lt;CONTEXT&gt;
                 &lt;value&gt;F&lt;/value&gt;
              &lt;/CONTEXT&gt;
              &lt;REFNO&gt;
                 &lt;value&gt;0039761&lt;/value&gt;
              &lt;/REFNO&gt;
           &lt;/ApiRequest&gt;
        &lt;/pms:pmsapi_nomination.getlist.ApiRequest&gt;
     &lt;/soapenv:Body&gt;
  &lt;/soapenv:Envelope&gt;
  </code></pre>
  
  <p>There is an SSL certificate on the system but not for port 7180.</p>
  
  <p>I'm also very much a powershell novice, I only write it a couple of times a year. </p>
  
  <p>So the fail is on</p>
  
  <pre><code>$resp = $soapWebRequest.GetResponse() 
  </code></pre>
  
  <p>but I guess that the setting up of $soapWebRequest may be the problem</p>