我在Flash客户端使用URLRequest从服务中获取一些信息,服务会检查请求url中的参数,如果参数无效,服务器可能会返回403内含一些http内容的内容,我需要分析内容,内容是json字符串:
{"code":3, "msg":"invalid channel"}
不幸的是,有时URLRequest无法从URLStream读取内容,stream.bytesAvailable为0,似乎URLStream读取了空数据。 URLRequest中是否有任何错误?或者只是因为我以错误的方式使用它?
以下是代码:
public function test() {
stream = new URLStream();
request = new URLRequest(url);
configureListeners(stream);
var timer:Timer = new Timer(2000, 10);
timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.start();
}
private function onTimer(e:TimerEvent):void {
request.url = url + "×tamp=" + getTimer();
trace("try to load: " + request.url);
stream.load(request);
}
private function configureListeners(dispatcher:EventDispatcher):void {
dispatcher.addEventListener(Event.COMPLETE, completeHandler);
dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
}
private function completeHandler(event:Event):void {
trace("completeHandler: " + event);
parseHeader();
}
private function httpStatusHandler(event:HTTPStatusEvent):void {
trace("httpStatusHandler: " + event);
}
private function ioErrorHandler(event:IOErrorEvent):void {
trace("ioErrorHandler: " + event.toString());
trace(stream.bytesAvailable + ", " + event.target.bytesAvailable + ", " + event.target.length);
}
这里是日志: enter image description here