WebService authentication issue

Hi

I have been having issues connecting to an internal webservice - 401 Unauthorized. I get this connecting from PowerShell and from a Project created using Visual Studio.

For some reason it looks like basic authentication is not being used so adding the following in the Visual Studio Project (Reference.cs created from wsdl file) works:

public partial class testServiceNoKeepAlive : testService
   {
       protected override System.Net.WebRequest GetWebRequest(Uri uri)
       {
           System.Net.HttpWebRequest request;
           request = (System.Net.HttpWebRequest)base.GetWebRequest(uri);
           request.KeepAlive = false;

           System.Net.NetworkCredential networkCredentials = Credentials.GetCredential(uri, "Basic");

           if (networkCredentials != null)

           { byte[] credentialBuffer = new UTF8Encoding().GetBytes(networkCredentials.UserName + ":" + networkCredentials.Password); request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(credentialBuffer); }

           else

           { throw new ApplicationException("No network credentials"); }

           return request;
       }

   }

Can something similar be done in PowerShell?

Thanks

Paul

Does the tip from jrv not help?
https://social.technet.microsoft.com/Forums/en-US/887fe68c-302a-474a-86b1-211e26349dbb/webservice-authentication-issue?forum=winserverpowershell#7a29fdbc-65c0-4e98-98b0-deec23ce40cf