views:

253

answers:

2

It seems like ASP.NET WebMethods are not "web servicey" enough to work with New-WebServiceProxy. Or maybe it is, and I haven't figured out how to initialize it?

So instead, I tried doing it manually, like so:

$wc = new-object System.Net.WebClient
$wc.Credentials = [System.Net.CredentialCache]::DefaultCredentials

$url = "http://www.domenicdenicola.com/AboutMe/SleepLog/default.aspx/GetSpans"
$postData = "{`"starting`":`"\/Date(1254121200000)\/`",`"ending`":`"\/Date(1270018800000)\/`"}"
$result = $wc.UploadString($url, $postData)

But this gives me "The remote server returned an error: (500) Internal Server Error." So I must be doing something slightly wrong.

Any ideas on how to call my PageMethod from PowerShell, and not get an error?

+2  A: 

Try the proxy approach again if indeed your are using a WebMethod. If so, the URL resource should have the extension .asmx but your's shows that you are using a standard ASP.NET page .aspx.

A proxy simplifies the use of a WebMethod quite a bit e.g.:

C:\PS>$URI = "http://www.webservicex.net/uszip.asmx?WSDL"

C:\PS> $zip = New-WebServiceProxy -uri $URI -na WebServiceProxy -class ZipClass

What sort of error are you getting when you try to use New-WebServiceProxy?

Keith Hill
I don't have an ASMX, because I have simply decorated a method in my code-behind with `[WebMethod]`. That is, there is SleepLog/Default.aspx, and then in SleepLog/Default.aspx.cs, there is a `WebMethod`.When I try to use `New-WebServiceProxy`, I thus get, predictably, "The HTML document does not contain Web service discovery information."
Domenic
@Domenic, why don't you create proper web service. It's as easy as inheriting from `System.Web.Services.WebService`. Then `New-WebServiceProxy` should work as Keith suggested in his should-be-accepted answer.
stej
Since I'm not really providing a service, web-service semantics seem inappropriate here. I'm just looking for a way to get the results of a specific POST call, which happens to be implemented as a `WebMethod` in a code-behind page. For my question, the accepted answer would be either "here's how to do that" or "that's not possible, and here's why."
Domenic
@Domenic If you just need to create a post, have a look at http://www.meadow.se/wordpress/?p=253 (simple) or http://stackoverflow.com/questions/2287714/how-to-post-to-vitalist-coms-api-via-powershell (quite verbose). On server side you may just use Response.Write('some xml'), read it in client and conver to xml. However, this is not a web service.
stej
A: 

Domenic - I am not a big PowerShell user, but the salient issue here is:

"PageMethods" are ScriptMethods and do not expose a WSDL or any other discovery vector and as such you must POST with a content-type application/json with post data urlencoded e.g. starting=[.net datetime string urlencoded]&ending=..... JSON encoding the input is incorrect.

Try using (HttpWebRequest)WebRequest.Create...... instead of WebClient, from which you would have to derive a class to enable changing the content type.

For instance, you could use something like this script and simply add a content type argument (or just hardcode it), something like this?

....
$req = [System.Net.HttpWebRequest]::Create($url);

$req.ContentType = "application/json";

$res = $req.GetResponse();
....
Sky Sanders