Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Send XML data to Web service

Posted on 2014-12-26
3
304 Views
Last Modified: 2015-01-07
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
0
Comment
Question by:Jatin Nahar
3 Comments
 
LVL 33

Accepted Solution

by:
ste5an earned 500 total points
ID: 40518391
An internal server error is what the error message says: an internal error. You can only fix it, when you have access to the running web service and it's code.

btw, have you tried using the web service the standard way? Maybe you're manually crafted XML is nonsense.

Test whether the service supports this by pasting this into your browser: https://abc.com/wenox/service.asmx?WSDL

When you'll get a result, then it's easy:
Create a new console or win forms project. Add a service reference to it in the context menu of the solution explorer.
Now you have an automatic generated proxy to this service.

When there is no answer, then when you have access to the compiled web service, use svcutil.exe to generate it.

Otherwise ask the owner of the service for the WSDL file.
0
 
LVL 16

Expert Comment

by:Rose Babu
ID: 40527314
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
0
 

Author Closing Comment

by:Jatin Nahar
ID: 40537226
excellent
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question