|
-
May 15th, 2008, 03:53 PM
#1
Thread Starter
Addicted Member
[02/03] Retrieve An XML File From HTTP POST
Hi,
I have an .aspx page that I use to collect data from another company. They send the data by calling the page with a HTTP GET and listing the datain a querystring. I pick up the values using the request.querystring method and all is fine. I want to change to another company who offer the data via posting an XML file to my page. How do I go about catching the information?
Any help much appreciated.
-
May 15th, 2008, 03:57 PM
#2
Re: [02/03] Retrieve An XML File From HTTP POST
-
May 15th, 2008, 04:19 PM
#3
Thread Starter
Addicted Member
Re: [02/03] Retrieve An XML File From HTTP POST
Hi,
Yes, I think that's what I want to do but I was hoping to do this in VB.Net rather than Javascript (I'm clueless on that!).
-
May 15th, 2008, 04:24 PM
#4
Re: [02/03] Retrieve An XML File From HTTP POST
may be user can upload the xml file using htmlFile upload control and at server side u can use xml classes to parse the data from it
-
May 16th, 2008, 03:16 AM
#5
Thread Starter
Addicted Member
Re: [02/03] Retrieve An XML File From HTTP POST
I have no control over the way the file is sent: they don't allow a choice. Is there any way to parse the file from the HTTP Post (using VB.Net)?
-
May 16th, 2008, 11:52 AM
#6
Re: [02/03] Retrieve An XML File From HTTP POST
One would assume that they've keyed the POST, in which case you should be able to use Request.Form[key] - for Java, but gives the basics: http://developers.sun.com/mobility/midp/ttips/HTTPPost/. Also worth reading: http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html.
Keep in mind that the files are not sent via HTTP; (binary) streams are. In this case, you need only extract the content from the request & create the appropriate file to store the content.
-
May 16th, 2008, 03:30 PM
#7
Re: [02/03] Retrieve An XML File From HTTP POST
A few things you want to look at are Request.BinaryRead, Posting Acceptor and sharing the solution with us when you're done.
Once you get the actual binary data, the rest is easy. You can create a file out of it using FileStream, (I assume you want to save the XML file somewhere for your own records), and then load it into an XmlDocument object. You can then read the nodes from it, but you should really figure out how to handle the upload part first.
-
May 19th, 2008, 08:39 AM
#8
Thread Starter
Addicted Member
Re: [02/03] Retrieve An XML File From HTTP POST
Thanks guys, I've a few things to try now, I'll show you the solution when (if!!) it works.
-
May 19th, 2008, 12:23 PM
#9
Re: [02/03] Retrieve An XML File From HTTP POST
 Originally Posted by dec_obrien
Thanks guys, I've a few things to try now, I'll show you the solution when (if!!) it works.
I'm interested to have a look at it as well, when and if you finish it.
-
May 23rd, 2008, 04:48 PM
#10
Thread Starter
Addicted Member
Re: [02/03] Retrieve An XML File From HTTP POST
At last, this is what I have. I think it works (the XML part definitely works). I could do with a code snippit to post a file from another aspx page to this one to test it fully though!!
Code:
Dim _XmlDoc As New XmlDocument()
Dim f_Files As HttpFileCollection
Dim iFileCount As Int32
Dim f_PostedFile As HttpPostedFile
Dim xOriginator As String = String.Empty
Dim xMessage As String = String.Empty
f_Files = Request.Files
Dim xpathDoc As XPathDocument
Dim xmlNav As XPathNavigator
Dim xmlNI As XPathNodeIterator
For iFileCount = 0 To f_Files.Count - 1
f_PostedFile = f_Files(iFileCount)
xpathDoc = New XPathDocument(f_PostedFile.InputStream)
'xpathDoc = New XPathDocument(f_PostedFile.InputStream)
'Retrieve the record
xmlNav = xpathDoc.CreateNavigator()
xmlNI = xmlNav.Select("info")
xOriginator = xmlNI.Current.Value
xmlNav = xpathDoc.CreateNavigator
xmlNav.MoveToRoot()
'Pick Up Originator
xmlNI = xmlNav.SelectDescendants("originator", String.Empty, False)
xmlNI.MoveNext()
xOriginator = xmlNI.Current.Value
'Pick Up Message
xmlNI = xmlNav.SelectDescendants("message", String.Empty, False)
xmlNI.MoveNext()
xMessage = xmlNI.Current.Value
Next
This is the XML file
HTML Code:
<?xml version='1.0'?>
<info>
<originator>testOriginator</originator>
<recipient>XXXXX</recipient>
<date>XXXXX</date>
<message>This is a Test Message</message>
</info>
-
May 23rd, 2008, 04:59 PM
#11
Re: [02/03] Retrieve An XML File From HTTP POST
Request.Files, that was it!!
I knew there was something I was forgetting, but this was from long ago. Anyways, to test your code, you will need to set the enctype in your <form> tag.
Code:
<form method="post" enctype="multipart/form-data"
action="http://example.com/yourfile.aspx">
<input type="file" name="file1" id="file1" />
<br/>
<input type="file" name="file2" id="file2" />
<br/>
<input type="submit" value="Go" id="Go" name="Go" />
</form>
Without it, Request.Files won't work. I suppose it'd be worth having a word with them to ensure that the encoding type is indeed multipart/form-data.
-
May 23rd, 2008, 05:09 PM
#12
Thread Starter
Addicted Member
Re: [02/03] Retrieve An XML File From HTTP POST
Hi, I don't fully understand!! (it's 11pm on Friday night!!!)
If I use <form method="post" enctype="multipart/form-data" action="http://example.com/yourfile.aspx"> on Page1 (sender page), do I need to include the enctype="multipart/form-data" on Page2 (catcher page) as well? I tried something very similar to this last night but it didn't seem to call Page2. I know that there were some pedantics about removing the runat=server attribute from the form tag etc.
Sorry for the extra questions, but as they say over here "fair exchange is no robbery" (taking credit for the request.files!!)
-
May 23rd, 2008, 05:14 PM
#13
Re: [02/03] Retrieve An XML File From HTTP POST
No, you only need to mention this in the <form> tag on the first page. Put the code I gave you in one page (and change the action to point to your page), then try uploading your XML files.
If you're doing this from an ASP.NET page, then make sure you have the action in there as well, and a normal Submit button. In fact, you can take what I gave you in my post above and put that into a .html file, you don't even need to make it an ASP.NET page.
-
May 23rd, 2008, 05:14 PM
#14
Re: [02/03] Retrieve An XML File From HTTP POST
I just tested it out and it works fine with your code, I'm able to see two files in Request.Files.
-
May 23rd, 2008, 05:22 PM
#15
Thread Starter
Addicted Member
Re: [02/03] Retrieve An XML File From HTTP POST
I don't know what I did differently last night but now it works perfectly.
Thanks very much for the help. It's much appreciated.
-
May 24th, 2008, 04:27 AM
#16
Re: [02/03] Retrieve An XML File From HTTP POST
Perhaps the euphoria induced adrenalin impaired your vision and you were typing <forum> instead of <form>. 
Either way, that's pretty good, I quite like it. I'm going to rate you for it and thanks for reminding me of Request.Files.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|