Solved

Send XML data to Web service

Posted on 2014-12-26
3
310 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
[X]
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
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

740 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