问题
How can I call a ASP .NET web service and pass parameters using the URL?
For example, the URL for the service is like,
http://[localhost]:31856/MySystem/MyAPI.asmx?op=getHeight
I need to pass two parameters a and b, I tried
http://[localhost]:31856/MySystem/MyAPI.asmx?op=getHeight?a=254&b=1
But failed.
Please advice.
Many Thanks,
回答1:
If you need to pass more than one parameter, use this format param1=value1¶m2=value2
and so on.So your link should be:
http://[localhost]:31856/MySystem/MyAPI.asmx/AnyMethodName?op=getHeight&a=254&b=1
You need a method like this.This method returns a list of strings,its just for demonstration.
[WebMethod]
public List<string> AnyMethodName(string op, string a, string b)
{
//Do whatever you want, get answer
return (ans.ToList());
}
回答2:
I had the same problem and I needed to add the following in my webconfig inside the system.web -tag:
<webServices>
<protocols>
<add name="HttpGet" />
</protocols>
</webServices>
The rest was pretty much like already mentioned (using the example from Ashwin's answer, just removed the op-parameter)
[WebMethod]
public List<string> AnyMethodName(string a, string b)
{
//Do whatever you want, get answer
return (ans.ToList());
}
After that I was able to call the webservice with the following (removed the op-parameter again):
http://localhost/MySystem/MyAPI.asmx/AnyMethodName?a=254&b=1
回答3:
Change the second ?
to &
. If you look at the page rendered by http://[localhost]:31856/MySystem/MyAPI.asmx
, it will show you how to call it as an HTTP GET
.
回答4:
It's not like that.
You have to declare the parameters in function.
For example here is a little example:
[WebMethod]
public string[] getVariables(string sop, string sgsm)
{ // do what you want ... }
Then when you call it
WebReference.Service1 service = new WebReference.Service1();
service.getVariables("foo", "blabla");
回答5:
Follow Below Steps
Step 1: Click on Add Service Reference and add reference of service . It creates reference in a partial class to the service and all the methods which you need to call the service under the project namespace.
Step 2: Add the same class in using ..
Step 3: Browse the class file generated under the service reference and get the name of it .
Step 4: Create a client like new Service1SoapClient("Service1Soap");
and then use the service as Method . You can pass the parameter (if requires) like the way you pass while calling one method from another .
来源:https://stackoverflow.com/questions/11189451/how-can-i-call-a-web-service-and-pass-parameters-using-the-url