Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having trouble figuring out how posting to a web service works. I have the following code inside my console app(client).

C#
// Create a request using a URL that can receive a post. 
            WebRequest request = WebRequest.Create("http://tempuri.org/HelloWorld");
            // Set the Method property of the request to POST.
            request.Method = "POST";
            // Create POST data and convert it to a byte array.
            string postData = "This is a test that posts this string to a Web server.";
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";
            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;
            // Get the request stream.
            Stream dataStream = request.GetRequestStream();
            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close();
            // Get the response.
            WebResponse response = request.GetResponse();
            // Display the status.
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Display the content.
            Console.WriteLine(responseFromServer);
            // Clean up the streams.
            reader.Close();
            dataStream.Close();
            response.Close();


And it seems to be running fine. And here is my code from my web service.

C#
namespace WebService1
{
    /// <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(string postData)
        {
            return postData;
            
        }

        
    }

}


Which also seems to be running fine. But when I post from the client i am not seeing the information on the web service. I do not know much about web services so it could be something simple. I am able to invoke the hello world method and enter a string value and then see the correct xml data but i'm not really sure where or how to see the data that i'm posting from the client. Please help and keep in mind that i'm a complete newbie. Thanks in Advance!
Posted
Comments
Richard C Bishop 15-May-13 11:58am    
How are you capturing the response on the client-side?
Dustin Prevatt 15-May-13 12:46pm    
As i said I am completely new to this but I believe this is how the response is coming in.
WebResponse response = request.GetResponse();

1 solution

Many possibilities why your code is not working.

1) WebService.Namespace - Have a look at below link to understand what does it means. Its not the url of your service.
http://msdn.microsoft.com/en-us/library/system.web.services.webserviceattribute.namespace.aspx

http://social.msdn.microsoft.com/forums/en-US/asmxandxml/thread/ad85151b-868e-4c30-9bfc-a0c0affb6827/

2) Figure out first, what is an url of your service?

3) Write a simple GET method and see if you can access your url in browser.

Fundamental concept you are using here is REST. Have a look at below link to understand, What is REST?
https://en.wikipedia.org/wiki/Representational_state_transfer

If you want to GET/POST data purely REST based then I recommend you to learn WCF REST Service.

http://msdn.microsoft.com/en-us/magazine/dd315413.aspx
 
Share this answer
 
Comments
Dustin Prevatt 15-May-13 12:45pm    
Thank you for your response. My question now is the 3)The GET Method.

I am assuming that since I am posting from the client then I need a get method within the web service. Do you have an example of a simple GET method?
RaisKazi 15-May-13 12:53pm    
Well, I would suggest you to consider WCF REST Service based on what you are doing. Last url has everything about WCF REST Service including GET method.

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