如何调用Web服务并使用URL传递参数

Daf*_*Dil 10 asp.net url web-services

如何调用ASP .NET Web服务并使用URL传递参数?

例如,服务的URL就像,

http://[localhost]:31856/MySystem/MyAPI.asmx?op=getHeight

我需要传递两个参数a和b,我试过了

http://[localhost]:31856/MySystem/MyAPI.asmx?op=getHeight?a=254&b=1

但失败了.

请指教.

非常感谢,

Ash*_*ngh 9

如果您需要传递多个参数,请使用此格式param1=value1&param2=value2等等.所以您的链接应该是:

http://[localhost]:31856/MySystem/MyAPI.asmx/AnyMethodName?op=getHeight&a=254&b=1
Run Code Online (Sandbox Code Playgroud)

你需要一个这样的方法.这个方法返回一个字符串列表,它只是为了演示.

    [WebMethod]
    public List<string> AnyMethodName(string op, string a, string b)
    {
       //Do whatever you want, get answer
        return (ans.ToList());
    }
Run Code Online (Sandbox Code Playgroud)


pun*_*nen 7

我遇到了同样的问题,我需要在 system.web -tag 内的 webconfig 中添加以下内容:

<webServices>
<protocols>
<add name="HttpGet" />
</protocols>
</webServices>
Run Code Online (Sandbox Code Playgroud)

其余的很像已经提到的(使用 Ashwin 的答案中的示例,只是删除了 op 参数)

   [WebMethod]
    public List<string> AnyMethodName(string a, string b)
    {
       //Do whatever you want, get answer
        return (ans.ToList());
    }
Run Code Online (Sandbox Code Playgroud)

之后,我可以使用以下命令调用 web 服务(再次删除 op 参数):

http://localhost/MySystem/MyAPI.asmx/AnyMethodName?a=254&b=1
Run Code Online (Sandbox Code Playgroud)


Kad*_*dir 2

不像那样。
您必须在函数中声明参数。例如,这里有一个小例子:

[WebMethod]
public string[] getVariables(string sop, string sgsm)
{ // do what you want ... }
Run Code Online (Sandbox Code Playgroud)

然后当你调用它时

WebReference.Service1 service = new WebReference.Service1();
service.getVariables("foo", "blabla");
Run Code Online (Sandbox Code Playgroud)