Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make http post request by passing xml using c#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.51k
    Comment on it

    Following function will expect a generic List and serialize into xml and make an http post request and after receiving response it will again deserialize xml into generic list:-

        public customer CreateOrder(customer objCustomerRequest)
        {
          customer objCustomer = new customer();
            if (objCustomerRequest == null)
            {
                throw new ArgumentNullException("composite");
            }
            else
            {
                List<customer> customerList = new List<customer>();
                XmlSerializer xmlSerializer = new XmlSerializer(objCustomer.GetType());
                StringWriter textWriter = new StringWriter();
    
                xmlSerializer.Serialize(textWriter, objCustomer);
    
                string objresult = getXMLString(textWriter.ToString(), "customers");
                StringReader XmlDeserializer = new StringReader(objresult);
    
                return (customer)xmlSerializer.Deserialize(XmlDeserializer);
                ///return js.Deserialize(objresult);
            }
        }
    
    
    
    public string getXMLString(string xmlContent, string url)
            {
                string Url;
                string sResult;
                Url = System.Configuration.ConfigurationManager.AppSettings["UserURl"] + url;
                var httpWebRequest = (HttpWebRequest)WebRequest.Create(Url);
                httpWebRequest.ContentType = "text/xml";
                httpWebRequest.Method = "POST";
                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    streamWriter.Write(xmlContent);
                    streamWriter.Flush();
                    streamWriter.Close();
                    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                    {
                        var result = streamReader.ReadToEnd();
                        sResult = result;
                    }
                }
                return sResult;
            }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: