472,133 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,133 software developers and data experts.

Posting XML to Server from ASP.NET/C# webapp

Hi All,

I am trying to build an class that will POST XML to a merchant Gateway. The
XML represents a customer transaction. I have no idea how to go about posting
anything other then a collection of name value pairs. Can someone please give
me a hand. I have been searching for a few days. I've come up with 1 very
vague VB example from 1999. I'm currently developing in .NET 2.0 and 1.1.
Thanks
Nov 12 '05 #1
5 8012


Rich Williams wrote:

I am trying to build an class that will POST XML to a merchant Gateway. The
XML represents a customer transaction. I have no idea how to go about posting
anything other then a collection of name value pairs.


If you have an XML file on your file system you could use the method
UploadFile of the WebClient.
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemNetWebClientClassTopic.asp>
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemNetWebClientClassUploadFileTopic1.asp>

If you want to create the XML while making the HTTP POST request you
could use the OpenWrite method of WebClient and them create an
XmlTextWriter on the stream and write out the XML as needed.
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemNetWebClientClassOpenWriteTopic1.asp>

Or instead of WebClient there is the more sophisticated HttpWebRequest
which has GetRequestStream which you could use too to create an
XmlTextWriter on the stream and write out the XML as needed.
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemNetHttpWebRequestClassGetRequestStreamT opic.asp>

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #2


Rich Williams wrote:

"If you want to create the XML while making the HTTP POST request you could
use the OpenWrite method of WebClient and them create an XmlTextWriter on the
stream and write out the XML as needed."

It seems to make sense but I'm just not sure if I can then retrieve a
response?


It is probably better to then look at WebRequest/HttpWebRequest to POST
the XML data and receive the response e.g.
HttpWebRequest httpRequest = (HttpWebRequest)
WebRequest.Create("http://example.com/2005/04/receiveXML.aspx");
httpRequest.Method = "POST";
// now use XmlWriter on httpRequest.GetRequestStream
// to write out the XML
// then use
HttpWebResponse httpResponse = (HttpWebResponse)
httpRequest.GetResponse();
// now use properties/method of the response to read out
// headers or the response stream
// for instance
XmlDocument responseXML = new XmlDocument();
responseXML.load(httpResponse.GetResponseStream()) ;

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #3
Hi Martin,

I am trying without success. The response from the server in question keeps
telling me it is not receiving anything. Does this look correct?

System.Net.HttpWebRequest httpRequest =
(System.Net.HttpWebRequest)System.Net.WebRequest.C reate("https://esqa.moneris.com:443/gateway2/servlet/MpgRequest");
httpRequest.Method = "POST";
httpRequest.ContentType ="text/xml";

System.Xml.XmlTextWriter xmlWriter = new
System.Xml.XmlTextWriter(httpRequest.GetRequestStr eam(),
System.Text.Encoding.UTF8);

xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("request");
xmlWriter.WriteElementString("store_id", store_id);
xmlWriter.WriteElementString("api_token", api_token);
xmlWriter.WriteStartElement("purchase");
xmlWriter.WriteElementString("order_id",
System.Guid.NewGuid().ToString());
xmlWriter.WriteElementString("amount", amount);
xmlWriter.WriteElementString("pan",
CCNumberBox.Text.Trim());
xmlWriter.WriteElementString("expdate",
MonthList.SelectedValue + YearList.SelectedValue);
xmlWriter.WriteElementString("crypt_type", crypt);
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Close();

Maybe i am sending in the wrong Encoding? I've tried writing the xml to file
and it works and looks correct. I'm just not sure that it's writing to the
post stream?Thanks again for your help.

Rich

"Martin Honnen" wrote:


Rich Williams wrote:

"If you want to create the XML while making the HTTP POST request you could
use the OpenWrite method of WebClient and them create an XmlTextWriter on the
stream and write out the XML as needed."

It seems to make sense but I'm just not sure if I can then retrieve a
response?


It is probably better to then look at WebRequest/HttpWebRequest to POST
the XML data and receive the response e.g.
HttpWebRequest httpRequest = (HttpWebRequest)
WebRequest.Create("http://example.com/2005/04/receiveXML.aspx");
httpRequest.Method = "POST";
// now use XmlWriter on httpRequest.GetRequestStream
// to write out the XML
// then use
HttpWebResponse httpResponse = (HttpWebResponse)
httpRequest.GetResponse();
// now use properties/method of the response to read out
// headers or the response stream
// for instance
XmlDocument responseXML = new XmlDocument();
responseXML.load(httpResponse.GetResponseStream()) ;

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Nov 12 '05 #4
Rich Williams wrote:
Hi Martin,

I am trying without success. The response from the server in question
keeps telling me it is not receiving anything. Does this look correct? [...] Maybe i am sending in the wrong Encoding? I've tried writing the xml
to file and it works and looks correct. I'm just not sure that it's
writing to the post stream?Thanks again for your help.


Try using a debugging proxy like Fiddler (www.fiddlertool.com) to see
what's really going over the wire.

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 12 '05 #5


Rich Williams wrote:

I am trying without success. The response from the server in question keeps
telling me it is not receiving anything. Does this look correct?

System.Net.HttpWebRequest httpRequest =
(System.Net.HttpWebRequest)System.Net.WebRequest.C reate("https://esqa.moneris.com:443/gateway2/servlet/MpgRequest");
httpRequest.Method = "POST";
httpRequest.ContentType ="text/xml";

System.Xml.XmlTextWriter xmlWriter = new
System.Xml.XmlTextWriter(httpRequest.GetRequestStr eam(),
System.Text.Encoding.UTF8);

xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("request");
xmlWriter.WriteElementString("store_id", store_id);
xmlWriter.WriteElementString("api_token", api_token);
xmlWriter.WriteStartElement("purchase");
xmlWriter.WriteElementString("order_id",
System.Guid.NewGuid().ToString());
xmlWriter.WriteElementString("amount", amount);
xmlWriter.WriteElementString("pan",
CCNumberBox.Text.Trim());
xmlWriter.WriteElementString("expdate",
MonthList.SelectedValue + YearList.SelectedValue);
xmlWriter.WriteElementString("crypt_type", crypt);
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Close();


Looking over the code I don't see anything wrong in regard to the aim of
making a HTTP POST request sending XML as the HTTP request body. Of
course I don't know what kind of XML the server is looking for.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by VapidGav | last post: by
reply views Thread by dhnriverside | last post: by
8 posts views Thread by bigjmt | last post: by
7 posts views Thread by benoit | last post: by
4 posts views Thread by =?Utf-8?B?c2hhZG93?= | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.