C# 如何通过 URL 将参数传递给 Web 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13050760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass parameters to web service via URL
提问by user1668123
I have web service that I can consume successfully, but I am sharing my webservice with someone else who wants to input the parameters via the URL eg: //localhost:12345/Lead.asmx?op=SendFiles&Id=1234678&Name=Joe&Surname=Kevin
我有可以成功使用的 Web 服务,但我正在与想要通过 URL 输入参数的其他人共享我的 Web 服务,例如://localhost:12345/Lead.asmx?op=SendFiles&Id=1234678&Name=Joe&Surname=Kevin
I added :
我补充说:
<webServices>
<protocols>
<add name="HttpGet"/>
</protocols>
</webServices>
to my Web.Config file and my SendFile.asmx.cs code looks like this:
到我的 Web.Config 文件和我的 SendFile.asmx.cs 代码如下所示:
namespace SendFiles
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://testco.co.za/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class SendFile : System.Web.Services.WebService
{
[WebMethod]
public bool PostToDB(LoadEntity _lead)
{
ConnectToSQLDB(ConfigurationManager.AppSettings["Server"], ConfigurationManager.AppSettings["DB"],
ConfigurationManager.AppSettings["UserName"], ConfigurationManager.AppSettings["Password"], ref connectionRef);
if (LI.ImportFiles(_lead, ref (error)) == true)
{
return true;
}
else
return false;
}
I tried adding :
我尝试添加:
[OperationContract]
[WebGet]
bool PostToDB(string IDNo, string FName, string SName);
But I get an error that I must declare a body because it is not marked abstract, extern or partial. Can anyone help?
但是我收到一个错误,我必须声明一个主体,因为它没有标记为抽象的、外部的或部分的。任何人都可以帮忙吗?
采纳答案by michaelalm
In response to your request on how to create a WCF Rest Service...
响应您关于如何创建 WCF Rest Service 的请求...
In your service contract:
在您的服务合同中:
[ServiceContract]
public interface ITestService
{
[WebGet(UriTemplate = "Tester")]
[OperationContract]
Stream Tester();
}
On your implementation
关于你的实施
public class TestService : ITestService
{
public Stream Tester()
{
NameValueCollection queryStringCol = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters;
if (queryStringCol != null && queryStringCol.Count > 0)
{
string parameters = string.Empty;
for (int i = 0; i < queryStringCol.Count; i++)
{
parameters += queryStringCol[i] + "\n";
}
return new MemoryStream(Encoding.UTF8.GetBytes(parameters));
}
else
return new MemoryStream(Encoding.UTF8.GetBytes("Hello Jersey!"));
}
}
This simply prints out all your query string values. You can do whatever processing you'll need to do depending on what query string parameters you get.
这只是打印出所有查询字符串值。您可以根据获得的查询字符串参数进行任何需要进行的处理。
For example if you put in.
例如,如果您输入。
http://localhost:6666/TestService/Tester?abc=123&bca=234
Then you'll get
然后你会得到
123 234
123 234
As your output.
作为你的输出。
Here's the rest of the code if you still need it. this was built using a console app but it can easily be converted to web. The real import stuff are the one's above.
如果您仍然需要,这里是代码的其余部分。这是使用控制台应用程序构建的,但它可以轻松转换为网络。真正的进口东西是上面的。
class Program
{
static ServiceHost _service = null;
static void Main(string[] args)
{
_service = new ServiceHost(typeof(TestService));
_service.Open();
System.Console.WriteLine("TestService Started...");
System.Console.WriteLine("Press ENTER to close service.");
System.Console.ReadLine();
_service.Close();
}
}
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<services>
<service name="ConsoleApplication1.TestService">
<host>
<baseAddresses>
<add baseAddress="http://localhost:6666/TestService"/>
</baseAddresses>
</host>
<endpoint binding="webHttpBinding" contract="ConsoleApplication1.ITestService"
behaviorConfiguration="webHttp"/>
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="webHttpBinding" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
<readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647"/>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webHttp">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
回答by mckeejm
When you test via the test harness in the .asmx page, what URL is generated? Can you give that to your caller and verify their ability to execute the same url you did?
当您通过 .asmx 页面中的测试工具进行测试时,会生成什么 URL?你能把它给你的调用者并验证他们执行你所做的相同 url 的能力吗?
I would recommend a WCF REST based service if others using your service from non .NET clients is your main use case.
如果其他人从非 .NET 客户端使用您的服务是您的主要用例,我会推荐基于 WCF REST 的服务。

