473,394 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 8106


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Will Stuyvesant | last post by:
Bruce Eckel has a good article about using a browser as UI for applications you write: http://mindview.net/WebLog/log-0045 His example code is nice, he sets up a webserver *and is able to kill...
3
by: VapidGav | last post by:
Ok I am going a bit insane here. I have written a class which handles redirects to certain pages according to database settings. When I use the response.redirect from within my class I get this...
0
by: global | last post by:
Hi, can anyone help me I'm on Linux with UDB Runtime-Client 8.1.4 and try to connect to a windows udb-server 8.1.4 via Websphere and jdbc , and get this error: 3e1a29e5 WebGroup E...
0
by: dhnriverside | last post by:
Hi peeps I'm trying to access my project/solution files from Visual C#. The solution has been moved from my development server to the client's server, and I'm taking my machine in with me to the...
12
by: Ann Marinas | last post by:
Hi all, I would like to ask for some help regarding separating the asp.net webserver and the sql server. I have created an asp.net application for a certain company. Initially, we installed...
8
by: bigjmt | last post by:
Hi all, I'm new to this. This will be my first setup for a web app I created. I created a web app(form) in vb.net on my test machine. Now I want to host this solution on a web server machine. I...
3
by: dyczr | last post by:
Hello All, configuration: computer_1: IIS + Asp .Net Web App Web.config: <authentication mode="Windows" /> <identity impersonate="true"/> computer_2:
7
by: benoit | last post by:
Hi, if I write this code to retrieve a folder on the server Server.mappath("/DATA") I get this error message System.InvalidOperationException: Failed to map the path '/DATA' the virtual...
4
by: =?Utf-8?B?c2hhZG93?= | last post by:
I have a asp.net 2.0 web application created on a mapped web server drive, a very simple one with only one default page which is blank. But when I run it inside VS, the URL is...
1
by: viswanathtvs | last post by:
java.util.NoSuchElementException javax.faces.el.EvaluationException: java.util.NoSuchElementException at...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.