user3377627
user3377627

Reputation: 387

How can I use System.Net.WebClient like PowerShell New-WebServiceProxy?

The issue I am having connecting to a service via PowerShell, versus using C#, is that the service is expecting a key passed in the header.

I can use new-WebServiceProxy with the URI and have it pass the default credentials. I can then get the time-limited service key, but for subsequent calls to the service, there is no way to pass the key, which results in an error that there is no valid service key.

I know the cmdlet uses the System.Net.WebClient class and that had a settable headers property that I want to try using. I just can't seem to get that class to behave the same way as the cmdlet and return an object. Does anyone know how?

This is what I am using but which to implement in .Net directly from PowerShell:

$QMSService = New-WebServiceProxy -Uri http://localhost:4799/QMS/Service -Namespace QlikViewServer -UseDefaultCredential

That is the Powershell cmdlet I use to access the service. I would like to accomplish the same using:

$Client = New-Object System.Net.WebClient
$Client.UseDefaultCredentials = $true
$Client.BaseAddress = "http://localhost:4799/QMS/Service"

I just can't figure out how to create a proxy object that gives me access to all the members the service provides, using System.Net.WebClient. Once I set that, I can grab the TimeLimitedServiceKey and do this:

$ServiceKey = $ProxyObjectCreatedFromClient.GetTimeLimitedServiceKey()
$Client.Headers = "X-Service-Key", $ServiceKey

If someone could please help me on how to create the Proxy Object.

Upvotes: 1

Views: 2174

Answers (1)

thepip3r
thepip3r

Reputation: 2935

It appears that the GetTimeLimitedServiceKey() method is from the QMSClient API: https://help.qlik.com/en-US/qlikview-developer/12.1/apis/QMS%20API/html/2be1e405-a7e5-4a43-b1b6-9540b23a6226.htm

...meaning, you'll need to reference that third-party API directly (New-Object, Add-Type, Add-Type w/p/invoke, etc.) and then make the call to GetTimeLimitedServiceKey()

$service = New-WebServiceProxy -Uri http://someHost:4799/QMS/Service -Namespace QlikViewServer -Credential (get-credential)
$serviceKey = $service.GetTimeLimitedServiceKey()

https://community.qlik.com/thread/143003

Upvotes: 0

Related Questions