Google Answers Logo
View Question
 
Q: asp.net http post and receive xml back ( No Answer,   4 Comments )
Question  
Subject: asp.net http post and receive xml back
Category: Computers > Programming
Asked by: moxyman-ga
List Price: $120.00
Posted: 05 Apr 2006 18:50 PDT
Expires: 21 Apr 2006 08:53 PDT
Question ID: 715943
I have a few .aspx pages that I have written in Microsoft Visual
Studio 2005, and I want to post information on my form to a web
server.  The web server will only except data in xml format, and will
reply with an xml formated object.  I have the xml file replica that
the service receives, and teh replica file that the service sends
back.

My question, I dont know how to code aspx pages, only asp and im
running out of time to deliver this solution to my client.  I can make
teh forms on the aspx page, the thing i need is the server scripting
to:

user clicks a button that tells my server to process a script, that will..
- take the information from the form on the page and make a formatted xml object.
- post that xml document to the 3rd party web server
- receive the xml file back from teh 3rd party web server
- make the data from that received xml file available somehow to show
back to teh user.

This is a simple task, and I have it in asp, but now I have to have it
working in aspx!  I prefer VB, but C# is fine, i just need it.

Clarification of Question by moxyman-ga on 05 Apr 2006 18:55 PDT
This is the XML structure to be --SENT--
<epxml>
<header>
<responsetype>direct</responsetype>
<mid>99</mid>
<password>test</password>
<type>charge</type>
</header>
<request>
<charge>
<etan>6542335675</etan>
<card>
<cctype>visa</cctype>
<cc>4906383347771203</cc>
<expire>12/04</expire>
<cvv>648</cvv>
</card>
<cardholder>
<firstname>John</firstname>
<lastname>Doe</lastname>
<street>Anystreet</street>
<housenumber>12</housenumber>
<zip>12345</zip>
<city>Anytown</city>
<country>US</country>
<state>NY</state>
<email>any@emailadress.com</email>
</cardholder>
<amount>
<currency>EUR</currency>
<value>150</value>
</amount>
<product>
<name>Any product</name>
<url>http://any-url.com</url>
</product>
</charge>
</request>
</epxml>


This is the xml file to be  --received from 3rd party--
<epxml>
<header>
<responsetype>direct</responsetype>
<mid>99</mid>
<type>charge</type>
</header>
<response>
<success>
<etan>6542335675</etan>
<tan>1971</tan>
<message>amount successful charged</message>
</success>
</response>
</epxml>

I will send you the 3rd parties web server address when you request it.

Clarification of Question by moxyman-ga on 06 Apr 2006 10:01 PDT
i more then doubled the price, i hope its worth it for some of you to
supply what should be a quick solution.  Im not looking for all the
coding!!  Im looking for the code that will take the xml and send it
to a server, and receive the xml back..  and some information (like
hints on how to use it, not the actual code, just give me some
pointers) on how to view teh contents of the received xml.  But teh
biggie is simply sending the xml object and receiving the xml object.

Request for Question Clarification by hummer-ga on 06 Apr 2006 10:25 PDT
Hi moxyman,

I think this is what you need and it's nice because it includes explanations. 

Communicating with the server HTTPRequest
"One of the cool things about XMLHTTP is the ability to post XML data
to an HTTP server or get XML back from an HTTP server."
http://www.topxml.com/xml/learnxml.asp#httprequest

"Receiving an XML DOMDocument object in VB
In the VB part of this example, the code to connect to the web server is:

- create the objects
Dim xmlHttp As new XMLHTTPRequest
Dim xmlDoc As DOMDocument
- open the POST (or GET) connection to the web server
xmlHttp.Open "POST", "http://localhost/xmlcode/demo.asp", False
- establish the connection
xmlHttp.Send
- receive the response - note there are different types of responses,
binary and text is also supported
Set xmlDoc = xmlHttp.responseXML

On the web server side, we will use some ASP in the demo.asp file
which is being called.  Make sure your server side ASP file sets the
ContentType to "text/xml" and to send the XML back to the client, use:

<%
- make a variant object
Dim xmlDoc
- place the DOMDocument into the xmlDoc object
Set xmlDoc = Server.CreateObject("MSXML.DOMDocument")
- load the XML document object here
xmlDoc.load Server.MapPath("people.xml")
- set the content type to XML - note other types are supported
Response.contenttype = "text/xml"
- now send back the DOMDocument you have loaded above
response.write xmlDoc
%>

Now you have a simple VB application, which communicates with an ASP
file on a web server and receives a DOMDocument as a response."

How's that?
hummer

Clarification of Question by moxyman-ga on 06 Apr 2006 11:01 PDT
that code you supplied i have and works with vb in an asp page, but i
need it for an asp.net ".aspx" application.

the errors i show with the code provided: "xmlHttp declaration is
expected", "XMLHTTPRequest" is not defined, "DOMDocument is not
defined", and "set" is not uised anymore.


=============
- create the objects
Dim xmlHttp As new XMLHTTPRequest
Dim xmlDoc As DOMDocument
- open the POST (or GET) connection to the web server
xmlHttp.Open "POST", "http://localhost/xmlcode/demo.asp", False
- establish the connection
xmlHttp.Send
- receive the response - note there are different types of responses,
binary and text is also supported
Set xmlDoc = xmlHttp.responseXML
Answer  
There is no answer at this time.

Comments  
Subject: Re: asp.net http post and receive xml back
From: jiangsheng-ga on 06 Apr 2006 18:09 PDT
 
you need to import the System.Xml namespace:
Imports System.Xml 
, or simply refer the classes with the full name
System.Xml.XmlDocument and System.Net.HttpWebRequest
BTW, why not write a webservice for this?
Subject: Re: asp.net http post and receive xml back
From: moxyman-ga on 07 Apr 2006 12:05 PDT
 
Thanks for the pointer on using the namespaces.
I thought using a web service would be a bad way to go, since only one
web form would be making a request.  (so i figured just a single .vb
file would do it, or simply putting the code writein the header of the
aspx page.  Im not against anything at this point!  hehehe
Subject: Re: asp.net http post and receive xml back
From: ravishankar554-ga on 11 Apr 2006 00:23 PDT
 
Hi this is ravishankar,
         We can pass the xml documents to and from the aspx files
which are in different servers or in the same server. for that i
developed a code here in c#.
there are two webforms 
1) webform1.aspx
2) webform2.aspx
       and you have to set one tag in the web.config file as 
             <system.web> 
                   <pages validateRequest="false" />
               </system.web>
and also use system.xml name space.

webform1 code(in the page load you can write like this)
     private void Page_Load(object sender, System.EventArgs e)
	{
	if(Request.QueryString["file"]==null)
	{
	XmlDocument input = new XmlDocument();
	input.Load("c:/input.xml");
	string xmlfile = input.InnerXml;
	Response.Redirect("http://192.10.200.113/googleqa/WebForm2.aspx?file="+xmlfile);
	}
	else
	{
	XmlDocument output = new XmlDocument();
	string xmlstring =  Request.QueryString["file"].ToString();
	output.InnerXml = xmlstring;
	output.Save("c:/outputxml.xml");
	}
}
description:
      input.xml is the one which you want to pass to the 3rd party server
      outputxml.xml is the one which the 3rd party server is sending
you can do what ever me be the operations do you want on the outputxml
file.

webform2.aspx code
       	private void Page_Load(object sender, System.EventArgs e)
	{
	XmlDocument doc1 = new XmlDocument();
	string xmlstring =  Request.QueryString["file"].ToString();
	doc1.InnerXml = xmlstring;
	Response.Write("<xmp>"+xmlstring+"</xmp>");
	doc1.Save("c:/inputxml.xml");
	XmlDocument output = new XmlDocument();
	output.Load("c:/output.xml");
	string xmlfile = output.InnerXml;
	Response.Redirect("http://192.10.200.113/googleqa/WebForm1.aspx?file="+xmlfile);
	}
description
      so you are passing request to the webform2 from the webfrom1
with the uri contains one string variable. so you can access that
using Request.QueryString["file"] and convert the to xmldocument and
use that after that you send an another xml file in the same way as
you sent from the webform1. so here i included some ip address you can
change this to the required ip address.
  

             i think this solution is satisfy you
Subject: Re: asp.net http post and receive xml back
From: rick31-ga on 19 Apr 2006 11:07 PDT
 
this function should help ur problem. it works under ASP.net 1.1 and above.
just remember, u might need to add form filed name in the
requestcontent string if the post is based on form submission.
ie. requestcontent = "filedname=thexmlcontent"

Imports System.Net
Imports System.Text
Imports System.IO
Imports System.Xml

    Public Function GetHttpResponse(ByVal Uri As String, ByVal
RequestContent As String) As String

        Dim sRequest As HttpWebRequest = CType(WebRequest.Create(Uri),
HttpWebRequest)
        Dim encoding As New System.Text.UTF8Encoding

        Dim bByte As Byte() = encoding.GetBytes(RequestContent)
        'set request settings
        sRequest.Method = "POST"
        sRequest.Timeout = 10000
        sRequest.ContentType = "application/x-www-form-urlencoded"
        sRequest.ContentLength = bByte.Length

        Dim nStream As Stream = sRequest.GetRequestStream()
        'post the data
        nStream.Write(bByte, 0, bByte.Length)
        nStream.Close()

        'sRequest.AllowAutoRedirect = True
        Dim xResponse As String
        Dim sResponse As HttpWebResponse =
CType(sRequest.GetResponse(), HttpWebResponse)
        Dim outstrim As Stream = sResponse.GetResponseStream()
        Dim readStream As New StreamReader(outstrim, encoding)
        'read the response
        xResponse = readStream.ReadToEnd()
        sResponse.Close()
        Return xResponse
    End Function

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy