Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ALL,

I want to post one xml file using http post mehtod in to one URL.For that i need to develop code in c sharp.I'm using visual studio 2008 and asp.net 2.0.
Similarly i have to read the posted data from that URL.
Can anybody suggest any means or code sample or any helpful page so that i can develop this two web application and can test it by deploying in the local system.
Posted
Comments
Sandeep Mewara 18-Apr-11 11:25am    
Shouldn't you try before asking for code?

For now, it just sounds like a homework.
Sergey Alexandrovich Kryukov 18-Apr-11 13:32pm    
Yes, it does, but I decided to answer as there are couple of delicate aspects, and I know from some previous Questions that beginners try to use wrong code samples, so -- please see.
--SA
Sugato Pal 20-Apr-11 8:50am    
Thanks Sandeep for ur suggestion..I developed the code for my requirement.Actually i checked lot of web page but i'm not getting the proper solution..thats why i asked the question...
Sandeep Mewara 20-Apr-11 9:23am    
It might be. As I said earlier, 'for now, it just sounds like a homework'. What you forgot is to add and show what you tried and did. Had you done that, I am sure there would had been no confusion at all and others would have tried more to help you.

This MSDN sample shows how to perform the POST programmatically:

http://msdn.microsoft.com/en-us/library/debx8sh9.aspx[^].

See also the classes System.Net.HttpWebRequest and System.Net.WebRequest. To use Web request, you need both as run-time type of WebRequest is decided by the static method WebRequest.Create by the URL, like in

C#
HttpWebRequest httpRequest =
    (HttpWebRequest)WebRequest.Create("http://www.contoso.com/");


See also http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx[^], http://msdn.microsoft.com/en-us/library/system.net.webrequest.aspx[^].

—SA
 
Share this answer
 
v2
Comments
Espen Harlinn 18-Apr-11 17:47pm    
Good effort, my 5
Sergey Alexandrovich Kryukov 18-Apr-11 22:20pm    
Thank you, Espen.
--SA
Sugato Pal 20-Apr-11 8:51am    
Its really helpful to me.Thanks..my 5
Sergey Alexandrovich Kryukov 20-Apr-11 13:06pm    
Great. You're very welcome.
Are you going to formally accept this answer as well?
Thank you.
--SA
1st Application to POST the DATA:

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;

namespace PostExample
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Reading xml file from local directory
XmlTextReader reader = new XmlTextReader("C:\\SampleXml.xml");
XmlDocument xmlDoc = new XmlDocument();
//Load the file into the XmlDocument
xmlDoc.Load(reader);
//Close off the connection to the file.
reader.Close();
//Creating Request of the destination URL
HttpWebRequest httpRequest =(HttpWebRequest)WebRequest.Create("http://localhost/GetApplication/GetXmlData.aspx");

httpRequest.Method = "POST";

//Defining the type of the posted data as XML
httpRequest.ContentType = "text/xml";

string data = xmlDoc.InnerXml;
byte[] bytedata = Encoding.UTF8.GetBytes(data);
// Get the request stream.
Stream requestStream = httpRequest.GetRequestStream();
// Write the data to the request stream.
requestStream.Write(bytedata, 0, bytedata.Length);
requestStream.Close();
//Get Response
HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();

}
}
}

2nd Application to Read the posted data:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;

namespace GetExample
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Read the posted XML file
Stream myStream = Request.InputStream;
byte[] message = new byte[myStream.Length];
myStream.Read(message, 0, (int)myStream.Length);
string data = System.Text.Encoding.UTF8.GetString(message);
XmlDocument xmlDoc = new XmlDocument();
if ((data != ""))
{
xmlDoc.LoadXml(data);
string path = "C:\\SampleXmlE2E.xml";
xmlDoc.Save(path);//regenerates the xml file in different system.
}
}
}
}

Using this two application and IIS one can transfer data from one computer to another through HTTP POST method.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900