473,692 Members | 2,554 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 8149


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
1776
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
1589
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
1969
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
1317
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
2780
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
1320
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
1182
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
3386
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
3637
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
3900
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
8546
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8969
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8811
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,...
1
6462
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
5822
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
4327
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
4563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2981
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1962
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.