如何使用 XDocument 调用 Web 服务?

发布于 2024-08-14 07:58:04 字数 788 浏览 4 评论 0 原文

假设我有一个 asmx Web 服务,地址如下: http://localhost/BudgetWeb/Service.asmx

此 Web 服务具有一个具有以下签名的 Web 方法:

string GetValue(string key)

这个 GetValue 方法返回一个像这样的字符串:

<?xml version=\"1.0\" encoding=\"utf-8\" ?><value>250.00</value>

如果我想这样做怎么办:

XDocument doc = XDocument.Load("http://localhost/BudgetWeb/Service.asmx?op=GetValue&key=key1")

这不起作用,而且我很确定 XDocument.Load 实际上并没有调用服务器上的 Web 方法。我认为它期望 uri 指向它可以加载的文件。要调用 Web 方法,我想我必须有一个 Web 代理类,并且必须使用它来调用 string GetValue(string key),然后我可以使用从要传递给 XDocument.Load 方法的 Web 代理类。

我的理解是否正确,或者 XDocument.Load 有没有办法实际调用服务器上的 Web 方法?

Suppose I have an asmx web service at the following address:
http://localhost/BudgetWeb/Service.asmx

This web service has a web method with the following signature:

string GetValue(string key)

This GetValue method returns a string like this:

<?xml version=\"1.0\" encoding=\"utf-8\" ?><value>250.00</value>

What if I wanted to do this:

XDocument doc = XDocument.Load("http://localhost/BudgetWeb/Service.asmx?op=GetValue&key=key1")

This doesn't work, and I'm pretty sure that XDocument.Load doesn't actually invoke a web method on the server. I think it expects the uri to point to a file that it can load. To call a web method, I think I'd have to have a web proxy class and would have to use that to call string GetValue(string key), and then I could use that value returned from the web proxy class to pass to the XDocument.Load method.

Is my understanding correct, or is there a way for XDocument.Load to actually invoke a web method on the server?

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

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

发布评论

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

评论(2

对风讲故事 2024-08-21 07:58:04

尝试使用这个:

XDocument doc = XDocument.Load(
        "http://localhost/BudgetWeb/Service.asmx/GetValue?key=key1");

编辑:刚刚发现:你使用的是无效的URI:

http://localhost/BudgetWeb/Service.asmx?op=GetValue&key=key1

我应该

http://localhost/BudgetWeb/Service.asmx/GetValue?key=key1

使用这个代码片段:

string uri = "http://www.webservicex.net/stockquote.asmx/GetQuote?symbol=MSFT";
XDocument doc1 = XDocument.Load(uri);
Console.WriteLine(doc1.Root.Value);  // <StockQuotes><Stock><Symbol>MSFT...

Try to use this:

XDocument doc = XDocument.Load(
        "http://localhost/BudgetWeb/Service.asmx/GetValue?key=key1");

EDIT: Just figured out: you're using a invalid URI:

http://localhost/BudgetWeb/Service.asmx?op=GetValue&key=key1

Should be

http://localhost/BudgetWeb/Service.asmx/GetValue?key=key1

I'm using this code snippet:

string uri = "http://www.webservicex.net/stockquote.asmx/GetQuote?symbol=MSFT";
XDocument doc1 = XDocument.Load(uri);
Console.WriteLine(doc1.Root.Value);  // <StockQuotes><Stock><Symbol>MSFT...
盗琴音 2024-08-21 07:58:04

好的,我发现了问题。在 Web 服务的 web.config 中,您必须添加以下内容:

<webServices>
    <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
    </protocols>
</webServices>

感谢大家的建议,我真的很感激,特别是 Rubens Farias,他的工作示例让我走上了正轨。

Ok, I found the issue. In the web.config for the web service, you have to add this:

<webServices>
    <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
    </protocols>
</webServices>

Thanks to everyone for their suggestions, I really appreciate it, especially Rubens Farias whose working example put me on the right track.

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