Click here to Skip to main content
15,887,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C#
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.
        client.DefaultRequestHeaders.Accept.Add(
        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);
            }
        }
        else
        {
            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
Updated 19-May-17 12:53pm

1 solution

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

C#
public string GetRequest([FromBody]string value)
    {


needs to look like

C#
[HttpGet]
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]
 
Share this answer
 
Comments
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)



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