Thursday, May 12, 2016

Post XML over HTTP and capture the response – C#

Recently we got a requirement to post the data from ‘CRM Plug-in’ to an external API and capture response.External API was built as XML over HTTP (i.e., Its not a SOAP based and no WSDL). In this approach we post the Request XML to a URL and in return we will get the Response.

public static string PostXMLData(IOrganizationService orgService, ITracingService tracingService, string obmXMLString, string url, string userName, string password)
        {

            tracingService.Trace("Creating WebRequest Object." + url);
            WebRequest myWebRequest = WebRequest.Create(url);
            tracingService.Trace("Completed creating WebRequest Object." + url);

            tracingService.Trace("Setting WebRequest Object -> Method property.");
            // Set 'Preauthenticate'  property to true.  Credentials will be sent with the request.
            tracingService.Trace("Setting Preauthenticate to true");
            myWebRequest.PreAuthenticate = true;
            tracingService.Trace("Completed Setting Preauthenticate to true");

            // Create a New 'NetworkCredential' object.
            tracingService.Trace("Creating the Network credentials");
            NetworkCredential networkCredential = new NetworkCredential(userName, password);
            tracingService.Trace("Creating credential object to hold the id and password");
            string credentials = userName + ":" + password;
            tracingService.Trace("Completed creating credential object to hold the id and password");

            tracingService.Trace("Creating authorization object to hold the credentials");
            string authorization = Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials));
            tracingService.Trace("Completed creating authorization object to hold the credentials");

            tracingService.Trace("Setting the servicepointmanager security to SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12");
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
            tracingService.Trace("Completed setting the servicepointmanager security to SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12");
            tracingService.Trace("Completed creating authorization object to hold the credentials");


            tracingService.Trace("Setting WebRequest object Header");
            myWebRequest.Headers["Authorization"] = "Basic " + authorization;
            tracingService.Trace("Complted setting WebRequest object Header");

            tracingService.Trace("Setting WebRequest object Credentials property");
            myWebRequest.Credentials = networkCredential;
            tracingService.Trace("Completed Creating the Network credentials");

            myWebRequest.Method = "POST";
            tracingService.Trace("Completed Setting WebRequest Object -> Method property.");

            tracingService.Trace("Creating byte Array Object to hold xmlString.");
            byte[] byteArray = Encoding.UTF8.GetBytes(obmXMLString);
            tracingService.Trace("Completed creating byte Array Object to hold xmlString.");

            tracingService.Trace("Setting WebRequest Object ContentType Property.");
            //myWebRequest.ContentType = "application/xml";
            myWebRequest.ContentType = "text/xml";
            //hmyWebRequest.ContentType = "application/xml";
            tracingService.Trace("Completed setting WebRequest Object ContentType Property.");

            tracingService.Trace("Setting WebRequest Object ContentLength Property.");
            myWebRequest.ContentLength = byteArray.Length;
            tracingService.Trace("Completed Setting WebRequest Object ContentLength Property.");

            tracingService.Trace("Creating dataStream Object.");
            var dataStream = myWebRequest.GetRequestStream();
            tracingService.Trace("Completed Creating dataStream Object." + dataStream.ToString());

            tracingService.Trace("Write byteArray to dataStrean Object.");
            dataStream.Write(byteArray, 0, byteArray.Length);
            tracingService.Trace("Completed Write byteArray to dataStrean Object.");

            tracingService.Trace("Closing dataStream object.");
            dataStream.Close();
            tracingService.Trace("Completed Closing dataStream object.");

            tracingService.Trace("Creating response object.");
            var response = myWebRequest.GetResponse();
            tracingService.Trace("Completed Creating response object." + response.ToString());

            tracingService.Trace("Creating status description object.");
            var statusDesc = ((HttpWebResponse)response).StatusDescription;
            tracingService.Trace("Completed Creating status description object." + statusDesc.ToString());

            tracingService.Trace("Creating status code object.");
            var statusCode = ((HttpWebResponse)response).StatusCode;
            tracingService.Trace("Completed Creating status code object." + statusCode.ToString());

            tracingService.Trace("Set dataStream object with response.");
            dataStream = response.GetResponseStream();
            tracingService.Trace("Completed Set dataStream object with response." + dataStream.ToString());

            tracingService.Trace("Creating stream reader object.");
            var reader = new StreamReader(dataStream);
            tracingService.Trace("Completed Creating stream reader object." + reader.ToString());

            tracingService.Trace("Creating response string object.");
            var responseString = reader.ReadToEnd();
            tracingService.Trace("Completed Creating response string object:" + responseString);

            tracingService.Trace("Closing reader object.");
            reader.Close();
            tracingService.Trace("Completed Closing reader object.");

            tracingService.Trace("Closing dataStream object.");
            dataStream.Close();
            tracingService.Trace("Completed Closing dataStream object.");

            tracingService.Trace("Closing response object.");
            response.Close();
            tracingService.Trace("Completed Closing response object.");

            return responseString;
        }

No comments:

Post a Comment

Get files of last hour in Azure Data Factory

  Case I have a Data Factory pipeline that should run each hour and collect all new files added to the data lake since the last run. What is...