A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Posting XML Data from Flash to .Net Cutting Off Data

  1. #1
    Junior Member
    Join Date
    Jan 2008
    Posts
    1

    Posting XML Data from Flash to .Net Cutting Off Data

    My setup:
    Flash 2.0 using MX 2004
    Target Flash Player 7.0
    ASP.Net v2.0

    I am having a strange problem sending a little XML-formatted data from Flash (ActionScript 2.0) to a .Net handler page (.Net 2.0 Framework). I keep losing characters from the beginning of the data I am trying to send.

    ActionScript Side:

    Code:
    var str:String = "<element attribute1='hello' attribute2='world' attribute3='johnny' />";
    var xmlQuestionRequest:XML = new XML(str);
    xmlQuestionRequest.send("test.aspx", "_blank");
    When I trace this within flash, I get (not surprisingly):
    Code:
    <element attribute1="hello" attribute2="world" attribute3="johnny" />
    Now on the .Net side, things start to get weird. My .swf sends out the little XML packet and when it hits the test.aspx page, something happens.

    .Net Side

    Code:
    Sub Page_Load()
        Dim strPacket As String = ""
        strPacket = Server.UrlDecode(Request.Form(0))
        Response.Write(strPacket)
    End Sub
    After this runs, my output is this:
    Code:
    "hello" attribute2="world" attribute3="johnny" />
    So can you see what happened?

    This from Flash:
    Code:
    <element attribute1="hello" attribute2="world" attribute3="johnny" />
    Became this in .Net:
    Code:
    "hello" attribute2="world" attribute3="johnny" />
    It doesn't matter whether I URLDecode it or not, it comes out looking the same. In any case I lose:
    Code:
    <element attribute1=
    It doesn't matter if I use builder methods in ActionScript, either, or just a literal string as in this example. It also doesn't matter if I change the names of the attributes or the element. Also, and this is weird, if I add more attributes, I still lose the exact same thing from the beginning of the string! I always lose the opening < and the element name and the = sign.

    I have edited the code examples here down for simplicity and I might have made a little typo in the edit, but in the code I am using I am pretty sure it's typo-free.

    Does anybody have any idea about why this is happening and how to fix it?

  2. #2
    Senior Member
    Join Date
    Oct 2005
    Posts
    148
    That is indeed strange. I spent an hour trying to figure out a solution, and I kept running into that same bug. Unless you really need to send the data as xml, you may want to use just simple get or post variables instead and send them to your aspx page. Then have your aspx parse the data in xml, which would be cleaner and faster than sending heavy xml data via GET or POST.

  3. #3
    Junior Member
    Join Date
    Jan 2008
    Posts
    1
    Thanks for taking a look at my code! I really appreciate that. I was stumped bad when I posted that. I do need it to be XML data because it's a variable number of records and the .Net handler can process multiple different types of commands sent to it from Flash, so I need the flexibility that using XML affords me. I eventually did get it working, after compulsively checking for days to see if anybody figured it out.

    Here's what works. This is the complete code for a .aspx page that does nothing other than parrot back the XML that Flash sends to it. That's isn't so exciting, but it's great for degugging because you can see exactly what the XML that Flash is sending out looks like without tracing it in Flash.

    Here's the page:

    <%@ Page Language="VB" ValidateRequest="false" ContentType="text/xml" %>
    <%@ Import Namespace="System.Xml" %>
    <%@ Import Namespace="System.IO" %>

    <script runat="server">
    Sub Page_Load()
    Dim strPacket As String = ""
    strPacket = Request.Form.ToString
    Dim decodedString As String = Server.UrlDecode(strPacket)
    Dim xmlDoc As XmlDocument = New XmlDocument()
    xmlDoc.LoadXml(decodedString)
    xmlDoc.Save(Response.Output)
    End Sub
    </script>
    There are two differences here, one is that I didn't use form(0) I used form instead, and the other is that I set the content type of the page to "text/xml" and outputed the document to the Response.Output stream.

  4. #4
    Senior Member
    Join Date
    Oct 2005
    Posts
    148
    Nice you got it working. Yeah it was a strange issue.

    BTW, there are other alternatives to communicate between flash and .NET. I tend to use Webservices since it's so easy to build in .NET and Flash has a Webservice class already built in so it can easily make consume web services. Or you can use the various open source Flash Remoting class out there. Like Flourine or AMF.NET. Flourine seems to be gaining some traction and it seems popular. I personally went with Web Services route and used JSON to pass the data back and forth as JSON is more compact than XML so less data to send, and the preferred choice for sending AJAX data. Unfortunately there is no native JSON class for both ActionScript and .NET 2.0 (although I think 3.5 has it), but there are great 3rd party classes already built.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center