Solved

Send XML data to Web service

Posted on 2014-12-26
3
289 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 32

Accepted Solution

by:
Stefan Hoffmann 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
.Net application crashing 6 20
Check my code out(2) 1 27
Problem to page 4 17
Cannot upload files above 1mb IIS7 11 0
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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now