Monday, December 12, 2011

How to send/receive SOAP request and response and return in dataset using C#

public Dataset GetCountries (string _agentCode, string _password)
       {
           WebRequest webRequest = WebRequest.Create("http://www.flexiblecarhire.com/flexibleservice/fchxmlinterface.asmx");
           HttpWebRequest httpRequest = (HttpWebRequest)webRequest;
           httpRequest.Method = "POST";
           httpRequest.ContentType = "text/xml; charset=utf-8";
           httpRequest.Headers.Add("SOAPAction: http://fchhost.org/GetCountries");
           Stream requestStream = httpRequest.GetRequestStream();
           //Create Stream and Complete Request           
           StreamWriter streamWriter = new StreamWriter(requestStream, Encoding.ASCII);


           StringBuilder soapRequest = new StringBuilder("");
           soapRequest.Append("");
           soapRequest.Append("" + _agentCode + "");
           soapRequest.Append("" + _password + "");
           soapRequest.Append("");

           streamWriter.Write(soapRequest.ToString());
           streamWriter.Close();
           //Get the Response  
           HttpWebResponse wr = (HttpWebResponse)httpRequest.GetResponse();
           StreamReader srd = new StreamReader(wr.GetResponseStream());
         
           DataSet ds = new DataSet();
           ds.ReadXml(wr.GetResponseStream());

           return ds;
       }

No comments:

Post a Comment