简体   繁体   English

如何发送ASP.NET webservice的参数

[英]How can I send parameters for ASP.NET webservice

I have an university project where I should implement a java powered website that uses web services: some that will be created as servlets and others that should be created as .NET "servlets". 我有一个大学项目,我应该实现一个使用Web服务的java驱动的网站:一些将作为servlet创建,另一些应该创建为.NET“servlets”。 I created the java servlet that can be called as /loginservice/username="__________"&md5="____________". 我创建了可以被称为/ loginservice / username =“__________”&md5 =“____________”的java servlet。 All good. 都好。 Now I must implement another service in .NET. 现在我必须在.NET中实现另一个服务。 I created a ASP.NET web service application but this type of application uses POST instead of GET. 我创建了一个ASP.NET Web服务应用程序,但这种类型的应用程序使用POST而不是GET。 I found out that this could be changed by adding 我发现这可以通过添加来改变

[ScriptMethod(UseHttpGet=true)]

but the problem is that I can't pass parameters like I do in Java. 但问题是我不能像在Java中那样传递参数。 There is no way of using POST anywhere because I don't want to rewrite the code in Java. 无法在任何地方使用POST,因为我不想在Java中重写代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;

namespace t5_services
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [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 Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        [ScriptMethod(UseHttpGet=true)]
        public string Package(String packagename, String lastname)
        {
            return "Hello " + packagename + ": " + lastname;
        }

    }
}

Here is the code in C# If I use the browser and manually insert the values all is ok. 这是C#中的代码如果我使用浏览器并手动插入值,则一切正常。

But I can't use the GET convention. 但我不能使用GET惯例。 Thank you in advance. 先感谢您。

I finally solve the problem by removing 我终于解决了这个问题

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]

and adding 并添加

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

to Web.config 到Web.config

Now I can call the service using 现在我可以使用调用该服务

http://localhost:2586/Service1.asm/HelloWorld?parameter1=abc&parameter2=cde

This is an example of how I do it in WCF. 这是我在WCF中如何做的一个例子。 I'm sure it will be very similar for your Asp.net service. 我相信你的Asp.net服务会非常相似。 If nothing else, it should point you in the right direction. 如果不出意外,它应该指向正确的方向。

This is your function declaration in your interface file. 这是您的接口文件中的函数声明。

[OperationContract]
[WebInvoke(Method = "GET",
    ResponseFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.Wrapped,
    UriTemplate = "DoWork?message={message}&message2={message2}")]
string DoWork(string message, string message2);

This goes into a class that implements that interface. 这将进入实现该接口的类。

    public string DoWork(string message, string message2)
    {
        return "foobar";
    }

Your GET request would then look something like http://yoursite.com/DoWork?message=param1&message2=param2 您的GET请求看起来像http://yoursite.com/DoWork?message=param1&message2=param2

Does this help? 这有帮助吗? Sorry for the short reply, just packing up to go home. 对不起,简短的回复,只是打包回家。

$.ajax & passing data to .asmx webservice $ .ajax并将数据传递给.asmx webservice

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

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