Click here to Skip to main content
15,886,518 members
Articles / Web Development / ASP.NET
Tip/Trick

.NET XML Input Parameter to RESTful API & ByPassing SSL Certificate

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
23 Oct 2015CPOL1 min read 15.1K   177   2  
XML input parameter to RESTful API using HttpClient and bypassing the SSL certificate

Introduction

This is a small tip that would help you to understand how to POST XML input parameter to RESTful API and get the response back in C#.

Using the Code

I am explaining main functions that actually are calling RESTful API and passing the XML parameter, you can find the complete running Windows Application in the download section.

btnInvokeWebAPI_Click button will load the XML file into XDocument object from the path specified in txtXMLPath textbox. You can also use XmlDocument class if you want.

ExecutePOSTWebAPI function will receive the RESTful API URL in resourceName and XDcoument object in classObj parameters.

If your RESTful API requires SSL certificate but you want to bypass it for testing, you can add the following line of code:

C#
ServicePointManager.ServerCertificateValidationCallback += 
		(sender, certificate, chain, sslPolicyErrors) => true;

httpContect variable has input XML converted into string with encoding type as UTF8 and content type as XML. PostAsync method of HttpClient object will post httpContent, in our case the input XML to RESTful API and will store the result in response variable.

The response variable would have StreamContent content type so we can use Content.ReadStringAsync() method to get the result in Task<string> format that later we are using to get the string and showing it in textarea txtResponse control.

C#
 private void btnInvokeWebAPI_Click(object sender, EventArgs e)
        {
            XDocument doc = XDocument.Load(txtXMLPath.Text.Trim());
            ExecutePOSTWebAPI(txtAPIAddress.Text.Trim(), doc);
        }

public void ExecutePOSTWebAPI(string resourceName, object classObj)
        {
            using (var client = new HttpClient())
            {
                //Bypass SSL Certificate if you want, otherwise remove this line of code
                ServicePointManager.ServerCertificateValidationCallback += 
                	(sender, certificate, chain, sslPolicyErrors) => true;

                try
                {
                    var httpContent = new StringContent
                    	(classObj.ToString(), Encoding.UTF8, "application/xml");
                    var response = client.PostAsync(resourceName, httpContent).Result;
                    var res = response.Content.ReadAsStringAsync();

                    switch (response.ReasonPhrase.ToLower())
                    {
                        case "ok":
                            txtResponse.Text = res.Result;
                            break;
                    }
                }
                catch (Exception exp)
                {
                    MessageBox.Show(exp.InnerException.Message);
                }
            }
        }

History

  • Created: 10/23/2015

License

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


Written By
Architect
United States United States
A Solutions Architect with more than fourteen years of experience in application development. I mostly work in .NET, Angular, MEAN stack technologies and love to share what I do and learn during my day to day job. Please check my tutorials and blog:
https://fullstackhub.io
https://fullstackhubblog.com

Comments and Discussions

 
-- There are no messages in this forum --