locked
Consume Web api for Get,post,delete,head,option,merge using xml as input RRS feed

  • Question

  • User-148788041 posted

    Hi,

    I have  the following:

    • End point url
    • Method - get or post or head or options etc
    • Method name - "api/GetEmployee"
    • Input string - example C::\get.xml or xml string in a text box
    • Response as dictionary or object

    Anand

    Tuesday, February 12, 2019 6:16 PM

All replies

  • User36583972 posted

    Guhananth

    Hi,

    I have  the following:

    • End point url
    • Method - get or post or head or options etc
    • Method name - "api/GetEmployee"
    • Input string - example C::\get.xml or xml string in a text box
    • Response as dictionary or object

    Anand

    I have made a sample on my side. You can refer it.

    Client:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SendXMLstring.aspx.cs" Inherits="API2_Temple.SendXMLstring" validateRequest="false"  Async="true"  %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="TextBox1" runat="server" Width="654px">&lt;XMLComputerInfo&gt;&lt;Processor&gt;AMD&lt;/Processor&gt;&lt;HardDrive&gt;Toshiba&lt;/HardDrive&gt;&lt;/XMLComputerInfo&gt;</asp:TextBox>
    &nbsp;<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Send XML" />
            </div>
        </form>
    </body>
    </html>

    Client.cs:

      protected async void Button1_Click(object sender, EventArgs e)
            {
                using (var httpClient = new HttpClient())
                {
                    var uri = new Uri(baseUrl + "/api/xmlsaa");
                    //var XML = "<XMLComputerInfo><Processor>AMD</Processor><HardDrive>Toshiba</HardDrive></XMLComputerInfo>";
                    var XML = TextBox1.Text;
                    var httpContent = new StringContent(XML, Encoding.UTF8, "text/xml");
                    var response = await httpClient.PostAsync(uri, httpContent);
                }
            }
    

    API Server:

     [HttpPost]
            public HttpResponseMessage ReceiveXML([FromBody] XMLComputerInfo result)
            {
                if (result != null)
                {
                    HttpResponseMessage response = new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
                    response.Content = new ObjectContent<XMLComputerInfo>(result, new XmlMediaTypeFormatter());
    
                    return response;
                }
                else
                {
                    HttpResponseMessage response = new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
                    response.Content = new ObjectContent<string>("null object", new XmlMediaTypeFormatter());
                    return response;
                }
    
            }
    

    WebApiConfig:

    public static void Register(HttpConfiguration config)
            {
                // Web API configuration and services
    
                // Web API routes
                config.MapHttpAttributeRoutes();
    
                GlobalConfiguration.Configuration.Formatters.Clear();
               
                //GlobalConfiguration.Configuration.Formatters.Add(new System.Net.Http.Formatting.JsonMediaTypeFormatter());
    
                GlobalConfiguration.Configuration.Formatters.Add(new System.Net.Http.Formatting.XmlMediaTypeFormatter());
    
                var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
                xml.UseXmlSerializer = true;
            }
    
       public class XMLComputerInfo
        {
            [XmlElement("Processor")]
            public string Processor { get; set; }
            [XmlElement("HardDrive")]
            public string HardDrive { get; set; }
        }
    


    Besides, It would be appreciated if you could close the thread by marking helpful posts as an answer. If you have a new question you can start a new thread with all necessary code snippets for anyone else to be able to reproduce your issue from scratch along with a detailed description about the results including any exception messages.

    Best Regards,

    Yong Lu

    Wednesday, February 13, 2019 2:53 AM
  • User-148788041 posted

    Hi,

    Will this work for any WebApi or Rest for xml as input?

    protected async void Button1_Click(object sender, EventArgs e)
            {
                using (var httpClient = new HttpClient())
                {
                    var uri = new Uri(baseUrl + "/api/xmlsaa");
                    //var XML = "<XMLComputerInfo><Processor>AMD</Processor><HardDrive>Toshiba</HardDrive></XMLComputerInfo>";
                    var XML = TextBox1.Text;
                    var httpContent = new StringContent(XML, Encoding.UTF8, "text/xml");
                    var response = await httpClient.PostAsync(uri, httpContent);
                }
            }

    Wednesday, February 13, 2019 4:44 PM
  • User36583972 posted

    Hi,

    Will this work for any WebApi or Rest for xml as input?

    protected async void Button1_Click(object sender, EventArgs e)
            {
                using (var httpClient = new HttpClient())
                {
                    var uri = new Uri(baseUrl + "/api/xmlsaa");
                    //var XML = "<XMLComputerInfo><Processor>AMD</Processor><HardDrive>Toshiba</HardDrive></XMLComputerInfo>";
                    var XML = TextBox1.Text;
                    var httpContent = new StringContent(XML, Encoding.UTF8, "text/xml");
                    var response = await httpClient.PostAsync(uri, httpContent);
                }
            }


    Hi Guhananth,

    There is no omnipotent code here, you need to change the code appropriately according to your own situation.

    Why don't you try it on your side? You need to try to write your own code based on Community members' advice.

    Besides, It would be appreciated if you could close the thread by marking helpful posts as an answer. If you have a new question you can start a new thread with all necessary code snippets for anyone else to be able to reproduce your issue from scratch along with a detailed description about the results including any exception messages.

    Best Regards,

    Yong Lu

    Thursday, February 14, 2019 2:43 AM