Quantcast
Channel: WCF, ASMX and other Web Services
Viewing all articles
Browse latest Browse all 555

WebMethod to accept XML from HTTP Post

$
0
0

2 days I've been on this and I don't think it should be too difficult, but it seems to be.

I am writing a web service to provide web methods. For example, I have a method called AddCustomer. It sits there and waits for a client to connect to it. The idea is that the client will send me an HTTP Post with XML as the content type. I am having trouble setting this up.

        [WebMethod]
        public string AddCustomer(string xmlCustomer)
        {
            return (String)PostCustomerInternal(xmlCustomer);
        }

I do not know if my parameter type is correct, so if I shouldn't be picking up a string, tell me. You can see this function just calls an internal function called PostCustomerInternal. I don't know why, but that is how I have been told they want it. Now, the next function:

        public string PostCustomerInternal(string xmlCustomer)
        {
            string documentContents = "";
            HttpContext context;

            using (Stream receiveStream = context.Request.InputStream) // This line has an error because it doesn't know what context is (neither do I, I found this code)
            {
                using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
                {
                    documentContents = readStream.ReadToEnd();
                }
            }

            return documentContents;
        }

The example XML I have been given that will be posted is this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><customersT><customer><custId>TEST1201</custId><custName>Test Company 1201</custName><groupCode>XXX</groupCode></customer></customersT>

It's actually a lot longer than that, but I removed tonnes of extra nodes. The point is, to make a customer record in my end of the system requires these 3 bits of data.

I even have a test class to put the data:

namespace WcfService1.Classes
{
    public class TestCust
    {
        public TestCust()
        {
        }

        public int Id { get; set; }
        public string CustId { get; set; }
        public string CustName { get; set; }
        public string GroupCode { get; set; }
    }
}

So, the question is, how do I get a WebMethod to pick up XML that is posted to it? It isn't being passed on the querystring. It is in the HTTP Header. To test, when I got the thing to compile earlier, I tried using PostMan to send it XML but it didn't like it. Currently using SoapUI to see if I have any more luck. I don't really want to have to write a test client to send it because I don't know how to do that either. Every error I get moans about SOAP. I am assuming that when I pass the XML that I have, it is converted into a SOAP envelope? I have been going round and round in circles with this and it should be easy. When I allow the function to take multiple string parameters instead of working with HTTP Posts with XML and run it up in Chrome I can see the different parameters listed and in the generated web page it asks me to fill in the blanks, and press a button called Invoke. I invoke it after putting in the data from the above XML and hey presto, the customer gets created, so the back end is working. How the hell and I supposed to work with XML? I've seen some great examples where a couple of lines of code reads an XML file and chucks the entire contents into an object from a class they've written. How can I put the data from my XML into the class I've written?

Please help, before I throw this machine out of the window.


Viewing all articles
Browse latest Browse all 555

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>