Link to home
Start Free TrialLog in
Avatar of Jatin Nahar
Jatin Nahar

asked on

Send XML data to Web service

Hi All,

I sending the XML data to webservice but i am getting the error "the remote server returned an error :(500) internal server error"

Below is my code. Please help me to solve this issue.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
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;


namespace testwebservices
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.CallWebService();
        }

        public void CallWebService()
        {
            try
            {
                var _url = "https://abc.com/wenox/service.asmx";
                var _action = "https://abc.com/wenox/service.asmx?op=RealTimeTransactionNCPDP";

                XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
                HttpWebRequest webRequest = CreateWebRequest(_url, _action);
                InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);

                
                IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);

               
                asyncResult.AsyncWaitHandle.WaitOne();

                
                string soapResult;
                using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
                {
                    using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
                    {
                        soapResult = rd.ReadToEnd();
                    }
                    Console.Write(soapResult);
                    label1.Text = soapResult.ToString();

                }
            }
            catch(WebException we)
            {
                throw we;
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        private static HttpWebRequest CreateWebRequest(string url, string action)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Headers.Add("SOAPAction", action);
            webRequest.ContentType = "text/xml;charset=\"utf-8\"";
           
            webRequest.Accept = "text/xml";
            webRequest.Method = "POST";
            return webRequest;
        }

        private XmlDocument CreateSoapEnvelope()
        {
            XmlDocument soapEnvelop = new XmlDocument();
            
            soapEnvelop.Load("Patient_Connect_New_RX_23_Dec_2014_13_00_43.xml");
            return soapEnvelop;
        }

        private void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
        {
            using (Stream stream = webRequest.GetRequestStream())
            {
                soapEnvelopeXml.Save(stream);
            }
        }
    }
}

Open in new window




thanks in advance
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Can you post the WSDL file for the service you are going to consume ?

as ste5an said, just get the WSDL content by running asmx service as like https://abc.com/wenox/service.asmx?WSDL

--
Rose
Avatar of Jatin Nahar
Jatin Nahar

ASKER

excellent