3

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". I created the java servlet that can be called as /loginservice/username="__________"&md5="____________". All good. Now I must implement another service in .NET. I created a ASP.NET web service application but this type of application uses POST instead of 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. There is no way of using POST anywhere because I don't want to rewrite the code in 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.

But I can't use the GET convention. Thank you in advance.

5
  • ASMX is a legacy technology, and should not be used for new development. WCF or ASP.NET Web API should be used for all new development of web service clients and servers. One hint: Microsoft has retired the ASMX Forum on MSDN. Commented Dec 30, 2013 at 19:50
  • Thank you for the info. The big problem is that Microsoft retires technologies too fast. For a newbie like me, it is really hard to find a good tutorial on a new subject. Also, I'm not really interested in Microsoft web technologies, but I have an university assignment, and I must obey the rules. Commented Dec 30, 2013 at 19:54
  • WCF is not a new technology. It's been here since 2006. Your university is far behind the times. Commented Dec 30, 2013 at 19:56
  • Maybe you deal everyday with WCF subjects. But for me, and perhaps my university this topic is still hot :)). The university curricula drives us from the beginning of the universe, if I could say so. :D. Commented Dec 30, 2013 at 20:02
  • Not to slight Señor Saunders, but Nucandrei is correct. WCF is hard to configure without very much in terms of performance gain, if you ask me. Though sometimes 10% is critical, most of the time, ASMX is much more forgiving to configure. Commented Oct 15, 2014 at 12:10

3 Answers 3

10

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

Now I can call the service using

http://localhost:2586/Service1.asm/HelloWorld?parameter1=abc&parameter2=cde
Sign up to request clarification or add additional context in comments.

2 Comments

Well done for finding a solution and thank you for posting it up here for future visitors.
For future visitors, Put webServices part inside <configuration> <system.web> tags.
1

This is an example of how I do it in WCF. I'm sure it will be very similar for your Asp.net service. 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

3 Comments

I created a WCF and write the code that you provided. When I enter localhost:1290/Service1.svc/DoWork?message1=acg&message2=msga a blank page appears.
Do you have httpGetEnabled=true in your web.config?
I found the solution. See below answer. One of the problems was that GET was disabled.
0

Does this help? Sorry for the short reply, just packing up to go home.

$.ajax & passing data to .asmx webservice

1 Comment

I needed a browser url to enter so I could check that service is working. The service will be used in JSF (JavaServerFaces) and has nothing to do with jQuery.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.