Quantcast
Channel: WCF, ASMX and other Web Services
Viewing all articles
Browse latest Browse all 555

sending data in XML format to a webservice

$
0
0

Hi,

I get an error when I send the XML data to a webservice.

I created a webservice and it has an "add" function.

[WebMethod]

public int add(int a, int b)
{
return a+b;
}

I added this webservice as a web reference to the website.

Website has two texboxes which allows user to enter values for a and b, and it has an "add" button .

here is the coed when you click on add button.

protected void Button1_Click(object sender, EventArgs e)
{
int a=Convert.ToInt32(TextBox1.Text);
int b=Convert.ToInt32(TextBox2.Text);
localhost.Service myWebService = new localhost.Service();
int c= myWebService.add(a, b);
TextBox3.Text = c.ToString();

}

This works absolutely fine. I could see the result in the 3rd text box in the website.

Instead of this I need to send the data (i.e the values a and b ) in XML format to the webservice without using SOAP protocol.

int a=Convert.ToInt32(TextBox1.Text);
int b=Convert.ToInt32(TextBox2.Text);
localhost.Service myWebService = new localhost.Service();

string xml_str = "";
string url_str = "";

xml_str = xml_str + "<?xml version='1.0' encoding='UTF-8'?>";
xml_str = xml_str + "<Addition>";
xml_str = xml_str + "<a>" + a.ToString();
xml_str = xml_str + "</a>";
xml_str = xml_str + "<b>" + b.ToString();
xml_str = xml_str + "</b>";
xml_str = xml_str + "<Result>" ;
xml_str = xml_str + "</Result>";
xml_str = xml_str + "</Addition>";

url_str = "http://localhost:57510/WebSite5/Service.asmx";   // THIS IS THE LINK TO WEBSERVICE WHICH HAS THE ADD FUNCTION

WebRequest request = WebRequest.Create(url_str);

byte[] bytes = Encoding.UTF8.GetBytes(xml_str);
request.ContentLength = bytes.Length;

Stream dataStream = request.GetRequestStream();

dataStream.Write(bytes, 0, bytes.Length);

dataStream.Close();

// This snippet to check if the webservice has received the data or not. its returns a 501 error here.

WebResponse webResponse = request.GetResponse();
dataStream = webResponse.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFields = reader.ReadToEnd();
reader.Close();
if (responseFields.ToString() == "SUCCESS") Response.Write("Hurray!");

Can somebody plz tell me

1. If this is the right way to pass XML data?

2. How do I receive the data in the webservice. I just have an add function in the webservice. Does it receive the data automatically? If not, do I have to write any function to recieve the data on the webservice? Also, the add function in the webservice has to be update the result in the XML data. I want XML data as output. 


Viewing all articles
Browse latest Browse all 555

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>