Click here to Skip to main content
15,905,233 members
Please Sign up or sign in to vote.
3.75/5 (4 votes)
See more:
Its my first time to deal with Web Services and I am having errors on consuming Web Methods with parameters. Below is my code.

[WebMethod]
		public string HelloWorld()
		{
			System.Diagnostics.EventLog.WriteEntry("HELLO WORLD INVOKED", "Congratulations, you have just invoked Hello World method of your local Web Service");
			return "Hello World";
		}
		[WebMethod]
		public string PostMethod(string test)
		{
			System.Diagnostics.EventLog.WriteEntry("POST METHOD INVOKED", "Congratulations, you have just invoked Post Method of your local Web Service. Data - " + test.ToUpper());
			return test.ToUpper();
		}


The way I am calling these web methods are by creating a HTTP POST to http://localhost/Service1.asmx/HelloWorld and http://localhost/Service1.asmx/PostMethod

I am able to invoke HelloWorld method successfully(I can verify it by looking at the Event Log) but when calling PostMethod I always get Internal Server Error. I also tried doing http://localhost/Service1.asmx/PostMethod?test=value but still no luck.

I know this can be solved by searching Google but since I am a beginner, I am not familiar on which direction to search. I tried though but no luck.

Appreciate your help.

[UPDATE]
This is how I post the data to the web service

XmlDocument _xml = new XmlDocument();
_xml.Load(filePath);

ServicePointManager.ServerCertificateValidationCallback = AcceptAllCertificatePolicy;
WebRequest _request = WebRequest.Create((WebServiceURL));
_request.Credentials = new NetworkCredential(UserName, Password);
_request.Method = "POST";
_request.ContentType = "text/xml";

XmlTextWriter xw = new XmlTextWriter(_request.GetRequestStream(), Encoding.UTF8);
_xml.WriteTo(xw);
xw.Close();
WebResponse response = _request.GetResponse();
response.Close();
Posted
Updated 25-May-11 19:33pm
v2

 
Share this answer
 
v2
Comments
samir.abda 25-May-11 23:19pm    
Thanks for the response. Going to the first article, it makes use of JQuery and JSON. However, Im looking for a solution that doesnt make use of them. As for the second one, I am not using BizTalk so I dont understand the article. Any other links please. Thanks in advance.
Wonde Tadesse 26-May-11 7:46am    
You shouldn't focus on the front end(consumer) applications. I don't think that is not your question.Just focus on the web service part.
You can access service method through ajax calling
//to allow call from script
[System.Web.Script.Services.ScriptService]
public class DummyService : System.Web.Services.WebService
{
   [WebMethod]
    public string DummyMethod(string id)
    {
        try
        {
              //TO DO: method implementation will goes here
              return id      
        }
        catch
        {
            return null;
        }
    }

   [WebMethod]
    public DummyModel DummyModelMethod(string id, string name)
    {
        try
        {
              //TO DO: method implementation will goes here
              return new DummyModel{Id=int.parse(id), Name=name};
        }
        catch
        {
            return null;
        }
    }
}
public class DummyModel 
{
   public int Id{set;get;}
   public int Name{set;get;}
}

Ajax calling helper
function jqAjaxPost(webmethod, paramArray, successFn, errorFn) {   
    //Create list of parameters in the form : {"paramName1":"paramValue1","paramName2":"paramValue2"}      
    var paramList = '';
    if (paramArray.length > 0) {
        for (var i = 0; i < paramArray.length; i += 2) {
            if (paramList.length > 0)
                paramList += ',';
            paramList += '"' + paramArray[i] + '":"' + paramArray[i + 1] + '"';
        }
    }
    paramList = '{' + paramList + '}';
    //Call the page method
    $.ajax({
        type: "POST",
        url: webmethod,
        contentType: "application/json; charset=utf-8",
        data: paramList,
        dataType: "json",
        success: successFn,
        error: errorFn
    });
}


we can invoke the service method by following way
<script type="text/javascript">
jqAjaxPost("DummyService.asmx/DummyMethod",
                  ["id", "1"],
                  function (msg) {
                      if (msg.d == null) {
                          alert("service error.");                         
                          return;
                      }
                      //alert("sucsess");
                      alert(msg.d);
                  },
                    function () {
                        alert('service method not found');                       
                    });
jqAjaxPost("DummyService.asmx/DummyModelMethod",
                  ["id", "1", "name", "hoho"],
                  function (msg) {
                      if (msg.d == null) {
                          alert("service error.");                         
                          return;
                      }
                      //alert("sucsess");
                      alert(msg.d.Id);
                      alert(msg.d.Name);
                  },
                    function () {
                        alert('service method not found');                       
                    });
</script>
 
Share this answer
 
Comments
samir.abda 26-May-11 1:36am    
I really appreciate the effort. Not to be mean, but, I commented on the other answer that I was looking for other solutions that are not JQuery and JSON. Would you be kind enough to provide other solution for this one? I have updated my question above on how I am posting data to the webservice. Thank you very much.
Hi
A few questions:

Did you get this sorted?

Have you tried breaking this down into something simpler (like not trying to send an xml stream of data, but just sending some simple text)?

I take it you're using Visual Studio. Have you tried 'Add Web Reference' to your project, and use that instance instead.

I've not used this way of invoking a Web Method, but when you use '_request.Method = "POST"' is that the method you are calling? And should that be _request.Method = "POSTMETHOD", or am I talking nonsense?

Just some thoughts. I have several web services in operation all day, every day using .Net technology.

Let me know

Julian
 
Share this answer
 
Comments
samir.abda 26-May-11 20:41pm    
Thanks for the answer. I haven't sorted this out yet. I forgot to mention that the web methods I have created are just for testing purposes. The real web service that I am about to consume is created in Java. As for your question about adding web reference in Visual Studio, Im not sure if this is possible. I know I can verify it myself, but unfortunately, there are no test web services here created in Java at the moment. I would appreciate if you provide your thoughts on this. As for your other statement about the _request.Method = "POST", its the way the team has created it to invoke the other Java web service. However, the web method has no parameters and unfortunately, the developer who created it is not in the team anymore. Any suggestions please. Thank you in advance.
So, questions:

Are you developing in Visual Studio?

And I understand from your reply that you don't have any web services to test with. Am I right? I'm confused, as how do you know that the HelloWorld call works?

I was just about to explain how I do it, but wasn't sure of your current set up. As far as I know, it shouldn't matter if the web service was created in Java or .Net or whatever. If you're developing the call to the web service in .Net I can help.

Julian
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900