Click here to Skip to main content
13,596,062 members
Rate this:
Please Sign up or sign in to vote.
I am trying to write a REST webservice api the will retreive xml data from a post request in another application.

What I have tried:

Post Request:
protected void Button1_Click(object sender, EventArgs e)
        string xmlMessage = "<?xml version=\"1.0\" encoding=\"UTF - 8\" ?>< PaymentNotification >< SessionID > 000001100913103301000000000001 </ SessionID > < RequestorID > 0000000001 </ RequestorID > < PayerPhoneNumber > 08060000000 </ PayerPhoneNumber > </ PaymentNotification >\r\n";
        // "construct your xml request message as required by that method along with parameters";
        string url = "http://localhost:8921/api/billpayment";

        postXMLData(url, xmlMessage);

    public void postXMLData(string destinationUrl, string requestXml)

        byte[] bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);

        var req = (HttpWebRequest)WebRequest.Create(destinationUrl);

        req.ContentType = "text/xml";
        req.Method = "POST";
        req.ContentLength = bytes.Length;

        using (Stream os = req.GetRequestStream())
            os.Write(bytes, 0, bytes.Length);

        string response = "";

        using (WebResponse resp = req.GetResponse())
            using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
                response = sr.ReadToEnd().Trim();

Get Request:

public class BillPaymentController : ApiController
    private const string URL = "http://localhost:8921/api/billpayment";
    private string urlParameters = "?api_key={55EB9B66-7B89-4FB2-AE6B-6FEAA78AC10C}";
    // POST api/billpayment
    // POST api/values
    public string GetRequest([FromBody]string value)

        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri(URL);

        // Add an Accept header for JSON format.
        new MediaTypeWithQualityHeaderValue("application/xml"));

        // List data response.
        HttpResponseMessage response = client.GetAsync(urlParameters).Result;  // Blocking call!
        if (response.IsSuccessStatusCode)
            // Parse the response body. Blocking!
            var dataObjects = response.Content.ReadAsAsync<IEnumerable<BillPaymentClass>>().Result;
            foreach (var d in dataObjects)
                Console.WriteLine("{0}", d.MerchantName);
            Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
        return "";


But i am having this error:

The remote server returned an error: (405) Method Not Allowed on the Post Request method, also the Get Request not working. tried to fix this, but no luck yet, any help will be appreciated
Posted 19-May-17 12:15pm
Updated 19-May-17 12:53pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Without having ran your code, I think the issue is you need to decorate your ApiControllers Action with the proper HTTP verb attribute.

public string GetRequest([FromBody]string value)

needs to look like

public string GetRequest([FromBody]string value)

And if you were trying to write an API action that utilized POST then you would add [HttpPost] instead of [HttpGet]

But one thing to note, for a Get request you'll need to remove the FromBody, so if this was meant to be a Post, then use [HttpPost]
Uwakpeter 20-May-17 3:43am
I have tried your suggestion yet no luck.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web04-2016 | 2.8.180621.3 | Last Updated 19 May 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100