Solved

Send XML data to Web service

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel file not created as expected 7 50
Data is not showing from images 15 37
C# HTTP GET method sample code 3 41
Javascript and Jquery not firing 9 40
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

12 Experts available now in Live!

Get 1:1 Help Now