AS3 URLStream cuts off first 4 characters of response

259 Views Asked by At

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

1

There are 1 best solutions below

0
On BEST ANSWER

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.