简体   繁体   English

如何通过URL将参数传递给Web服务

[英]How to pass parameters to web service via URL

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? 有人可以帮忙吗?

In response to your request on how to create a WCF Rest Service... 响应您关于如何创建WCF休息服务的要求...

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 123234

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>

When you test via the test harness in the .asmx page, what URL is generated? 当您通过.asmx页面中的测试工具进行测试时,会生成什么URL? Can you give that to your caller and verify their ability to execute the same url you did? 您可以将其提供给呼叫者,并验证他们执行与您执行的相同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的服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM