473,769 Members | 6,653 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8157


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/frlrfSystemNetW ebClientClassTo pic.asp>
<http://msdn.microsoft. com/library/default.asp?url =/library/en-us/cpref/html/frlrfSystemNetW ebClientClassUp loadFileTopic1. 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/frlrfSystemNetW ebClientClassOp enWriteTopic1.a sp>

Or instead of WebClient there is the more sophisticated HttpWebRequest
which has GetRequestStrea m 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/frlrfSystemNetH ttpWebRequestCl assGetRequestSt reamTopic.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.Crea te("http://example.com/2005/04/receiveXML.aspx ");
httpRequest.Met hod = "POST";
// now use XmlWriter on httpRequest.Get RequestStream
// to write out the XML
// then use
HttpWebResponse httpResponse = (HttpWebRespons e)
httpRequest.Get Response();
// now use properties/method of the response to read out
// headers or the response stream
// for instance
XmlDocument responseXML = new XmlDocument();
responseXML.loa d(httpResponse. GetResponseStre am());

--

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.Http WebRequest httpRequest =
(System.Net.Htt pWebRequest)Sys tem.Net.WebRequ est.Create("htt ps://esqa.moneris.co m:443/gateway2/servlet/MpgRequest");
httpRequest.Met hod = "POST";
httpRequest.Con tentType ="text/xml";

System.Xml.XmlT extWriter xmlWriter = new
System.Xml.XmlT extWriter(httpR equest.GetReque stStream(),
System.Text.Enc oding.UTF8);

xmlWriter.Write StartDocument() ;
xmlWriter.Write StartElement("r equest");
xmlWriter.Write ElementString(" store_id", store_id);
xmlWriter.Write ElementString(" api_token", api_token);
xmlWriter.Write StartElement("p urchase");
xmlWriter.Write ElementString(" order_id",
System.Guid.New Guid().ToString ());
xmlWriter.Write ElementString(" amount", amount);
xmlWriter.Write ElementString(" pan",
CCNumberBox.Tex t.Trim());
xmlWriter.Write ElementString(" expdate",
MonthList.Selec tedValue + YearList.Select edValue);
xmlWriter.Write ElementString(" crypt_type", crypt);
xmlWriter.Write EndElement();
xmlWriter.Write EndElement();
xmlWriter.Write EndDocument();
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.Crea te("http://example.com/2005/04/receiveXML.aspx ");
httpRequest.Met hod = "POST";
// now use XmlWriter on httpRequest.Get RequestStream
// to write out the XML
// then use
HttpWebResponse httpResponse = (HttpWebRespons e)
httpRequest.Get Response();
// now use properties/method of the response to read out
// headers or the response stream
// for instance
XmlDocument responseXML = new XmlDocument();
responseXML.loa d(httpResponse. GetResponseStre am());

--

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.d e
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.Http WebRequest httpRequest =
(System.Net.Htt pWebRequest)Sys tem.Net.WebRequ est.Create("htt ps://esqa.moneris.co m:443/gateway2/servlet/MpgRequest");
httpRequest.Met hod = "POST";
httpRequest.Con tentType ="text/xml";

System.Xml.XmlT extWriter xmlWriter = new
System.Xml.XmlT extWriter(httpR equest.GetReque stStream(),
System.Text.Enc oding.UTF8);

xmlWriter.Write StartDocument() ;
xmlWriter.Write StartElement("r equest");
xmlWriter.Write ElementString(" store_id", store_id);
xmlWriter.Write ElementString(" api_token", api_token);
xmlWriter.Write StartElement("p urchase");
xmlWriter.Write ElementString(" order_id",
System.Guid.New Guid().ToString ());
xmlWriter.Write ElementString(" amount", amount);
xmlWriter.Write ElementString(" pan",
CCNumberBox.Tex t.Trim());
xmlWriter.Write ElementString(" expdate",
MonthList.Selec tedValue + YearList.Select edValue);
xmlWriter.Write ElementString(" crypt_type", crypt);
xmlWriter.Write EndElement();
xmlWriter.Write EndElement();
xmlWriter.Write EndDocument();
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
1781
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 it too via a HTML button*. But his webserver does not do CGI. I have been trying to change the code with CGIHTTPRequestHandler and other such things, I have also tried various "recipes" for CGI webservers, but I can not get something that...
3
1594
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 error message Server Error in '/Application' Application. ---------------------------------------------------------------------------- ----
0
1977
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 SRVE0026E: -: java.lang.UnsatisfiedLinkError: SQLConnect at COM.ibm.db2.jdbc.app.DB2Connection.connect(DB2Connection.java:514) at COM.ibm.db2.jdbc.app.DB2Connection.<init>(DB2Connection.java:436)
0
1321
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 client. The problem is that the solution file says it cant find the projects. The sln file has a project as been at... http://server/webapp/webapp.csproj
12
2792
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 both the iis and sql server in a single machine. Not too long ago, the machine had some hardware problems, and management has decided to purchase new servers, for both asp.net and sql server.
8
1324
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 installed the framework on the server and copied the files that were in /Inetpub/wwwroot/webapp on my machine to the same location on the server. I then created a virtual directory in IIS on the server and mapped it to the location of the files.
3
1186
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
3392
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 folder for my webApp is not situated in Inetpub, nor is the DATA folder that I would like to retrieve
4
3645
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 http://localhost:1245/. Is this right or not? ALso the ASP.NET web site administration tool shows error in the Security tab as: There is a problem with your selected data store. This can be caused by an invalid server name or credentials, or by insufficient...
1
3906
by: viswanathtvs | last post by:
java.util.NoSuchElementException javax.faces.el.EvaluationException: java.util.NoSuchElementException at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102) at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) at javax.faces.component.UICommand.broadcast(UICommand.java:387) at...
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9997
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5309
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.