0

I have a URLStream object which accepts an XML formatted response but unfortunately the first 4 characters of the response are being chopped off, making the xml malformed.

The xml being returned from the server is correctly formatted if I use a URLLoader object.

Would anyone know how to fix this?

private var _stream:URLStream;

_myVariables = new URLVariables();

_myVariables.email = _un;
_urlRqSend = new URLRequest(_loginURL);

var encoder:Base64Encoder = new Base64Encoder();        
encoder.encode("password:"+_pw);

var credsHeader:URLRequestHeader = new URLRequestHeader("Authorization", "Basic " + encoder.toString());
_urlRqSend.requestHeaders.push(credsHeader);

_urlRqSend.data = _myVariables;         
_urlRqSend.method = URLRequestMethod.POST;      

_stream = new URLStream();          
_stream.addEventListener(flash.events.Event.COMPLETE, handleResponse);
_stream.load(_urlRqSend);



private function handleResponse(ev:flash.events.Event):void{    

trace("returned data: ",_stream.readUTFBytes(_stream.bytesAvailable));

}

Output

//returned data: l version="1.0"...etc

The correct response should be

//returned data: <?xml version="1.0"...etc

Thanks

crooksy88
  • 3,849
  • 1
  • 24
  • 30
  • Can you replace URLStream with URLLoader and when the response arrives to trace(_urlLoader.data) ? – Adrian Pirvulescu Nov 20 '12 at 08:39
  • you should first try it without the encoders to see if that's already good.. – csomakk Nov 20 '12 at 11:35
  • A guess would be that there's a byte order mark (BOM) at the beginning of the UTF-8 that the server spits out, which confuses readUTFBytes. Considering that Tamarin had bugs related to BOM detection as late as February this year, it's likely there are problems in the FlashPlayer implementation also. https://bugzilla.mozilla.org/show_bug.cgi?id=723461 – JimmiTh Nov 20 '12 at 15:49
  • Thank you all for your responses. Yes, URLLoader does return the XML correctly formatted. I guess I've either got to use this instead of URLStream or implement the workaround offered by csomakk. – crooksy88 Nov 20 '12 at 17:05

1 Answers1

0

here is an easy workaround:

var xml:String = _stream.readUTFBytes(_stream.bytesAvailable)
if(xml.search("<?xml version="1.0" != 0)){ //you may have to mix the "-s to compile.
  var start:int=xml.search("?>") + 3 //this will give you the character of the first element after the xml declaration (like <root> ...)  
  xml =  '<?xml version="1.0"...etc' + xml.Splice(start); //splice will return <root> ..., and we'll add the declaration before it.
}

I migth miscoded swomewhere, please use traces/debug to work, but something close to this will solve the issue. You should check it in mayor browsers to ensure its working okay everywhere.

csomakk
  • 5,369
  • 1
  • 29
  • 34