Search This Blog

Sunday, July 29, 2018

External SOAP Webservice consume in .net application connection Oracle 11g database

 .net Forms Application

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
//using Oracle.ManagedDataAccess.Client;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Net;
using System.IO;
using System.Net.Mail;
namespace WindowsFormsApplication3
{

        private void Submit_Click(object sender, EventArgs e)
        {

            Cursor.Current = Cursors.WaitCursor;
            runCode();
            Cursor.Current = Cursors.Default;
            //MessageBox.Show("No code inside :)");
        }

  public void runCode()
        {
            OracleConnection myConnection = new OracleConnection();
            myConnection.ConnectionString = "Data Source=TEST;User Id=usrname1;Password=test1";
            try
            {
                myConnection.Open();
            }
            catch (Exception ex)
            {
                SendEmail("Error check connection with database ->" + ex.Message);
            }
            //execute queries
            //OracleDataAdapter oda = new OracleDataAdapter("select * from xml_document", myConnection);
            try
            {
                OracleDataAdapter oda = new OracleDataAdapter("select apps.test_function_f() from dual", myConnection);//getting xml output from the function with 3 values ex: newnumber+'S'(success) + xml output.
                string l_msg = "";
                string l_refno = "";
                string l_xml = "";
                DataTable dt = new DataTable();
                oda.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    string output = dt.Rows[0][0].ToString();
                    // string test2 = "FUJREF1907201826";// dt.Rows[0][0].ToString();
                    string[] xx = new string[3];
                    xx = output.Split(',');
                    l_msg = xx[0];
                    l_refno = xx[1];
                    l_xml = xx[2];
                    // MessageBox.Show(l_refno);
                    this.textBox2.Text = l_refno.ToString();

                    if (l_msg == "S")
                    {
                        SOAP_Request(l_xml);
                    }
                    else
                    {
                        SendEmail("Error check the soap webservice ->" + l_msg + l_refno);
                    }


                    /* XmlDocument xmltest = new XmlDocument();
                     xmltest.LoadXml(test);
                     XmlNodeList elemlist = xmltest.GetElementsByTagName("soapenv:Envelope");

                     string result = elemlist[0].OuterXml.ToString();//.InnerXml;
                     XmlDocument xmltest1 = new XmlDocument();
                     xmltest1.LoadXml(result);
                     XmlNodeList elemlist1 = xmltest1.GetElementsByTagName("loc:SerId");
                     result = elemlist1[0].InnerXml.ToString();
                     MessageBox.Show(result);
                     MessageBox.Show(elemlist.Count.ToString());
                     */
                }
            }
            catch (Exception ex)
            {
                SendEmail("Error check for body exception ->" + ex.Message);
            }
            myConnection.Close();
        }

        //consume soap client
        public void SOAP_Request(string data)
        {
            HttpWebRequest request = CreateWebRequest();
            XmlDocument soapEnvelopeXml = new XmlDocument();
            soapEnvelopeXml.LoadXml(@data);
            using (Stream stream = request.GetRequestStream())
            {
                soapEnvelopeXml.Save(stream);
            }

            using (WebResponse response = request.GetResponse())
            {
                using (StreamReader rd = new StreamReader(response.GetResponseStream()))
                {
                    string soapResult = rd.ReadToEnd();
                    this.soapResult.Text = soapResult;
                    SendEmail(this.soapResult.Text);
                }
            }
        }

        //sending response to the emails from soap service
        public void SendEmail(String response)
        {
             MailMessage mail = new MailMessage("test@testmail.com", "test2@testmail.com");
            SmtpClient client = new SmtpClient();
            client.Port = 55;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Host = "10.1.1.1";//"smtp.gmail.com";
            mail.Subject = "Response from SOAP Webservice1->" + this.textBox2.Text;
            mail.Body = response;// "this is my test email body";
            client.Send(mail);
        }
       
        //soap link details
        public static HttpWebRequest CreateWebRequest()
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"http://test.com/services/EmpStatistics");
            webRequest.Headers.Add(@"SOAP:Action");
            webRequest.ContentType = "text/xml;charset=\"utf-8\"";
            webRequest.Accept = "text/xml";
            webRequest.Method = "POST";
            return webRequest;
        }
}
}

No comments:

Post a Comment