如何使用 PowerShell 调用 ASP.NET WebMethod?

发布于 2024-08-28 00:14:47 字数 591 浏览 8 评论 0原文

看起来 ASP.NET WebMethods 的“Web 服务性”不足以与 New-WebServiceProxy 一起使用。或者也许是这样,但我还没弄清楚如何初始化它?

因此,我尝试手动执行此操作,如下所示:

$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)

但这给了我“远程服务器返回错误:(500)内部服务器错误。”所以我一定做错了什么。

关于如何从 PowerShell 调用我的 PageMethod 并且不出现错误,有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人生百味 2024-09-04 00:14:47

如果您确实使用 WebMethod,请再次尝试代理方法。如果是这样,URL 资源应该具有扩展名 .asmx,但您的扩展名表明您正在使用标准 ASP.NET 页面 .aspx

代理大大简化了 WebMethod 的使用,例如:

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

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

当您尝试使用 New-WebServiceProxy 时,您会遇到什么类型的错误?

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?

森罗 2024-09-04 00:14:47

Domenic - 我不是 PowerShell 的大用户,但这里的突出问题是:

“PageMethods”是 ScriptMethods,不会公开 WSDL 或任何其他发现向量,因此您必须使用内容POST输入 application/json 并发布数据 urlencoded,例如,starting=[.net datetime string urlencoded]&ending=..... JSON 编码输入不正确。

尝试使用 (HttpWebRequest)WebRequest.Create...... 而不是 WebClient,您必须从中派生一个类才能更改内容类型。

例如,您可以使用类似 此脚本 的内容,并只需添加内容类型参数(或者只是硬编码它),类似这样的东西?

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

$req.ContentType = "application/json";

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

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();
....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文