I'm trying to Read the response of a http Head method request but i don't get anything as the response of head requests doesn't contains a body but i need to get the ['content-length'] which is a header returned this is my code
function GetSize() {
var request: URLRequest = new URLRequest(url);
request.method = URLRequestMethod.HEAD;
var loader: URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, DownloadFileSize, false, 0, true);
loader.load(request);
}
function DownloadFileSize(ev: Event) {
trace("the answer is :"+(URLLoader)(ev.currentTarget).data);
}
how can i fix this?
EVENT.COMPLETE
is just one of many events URLLoader can emit. For Example it can also emit HTTPStatusEvent.httpResponseStatus which possesses a property called responseHeaders
. Give that one a try.
Here's the example code provided by the documentation for HttpStatusEvent
package {
import flash.display.Sprite;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.HTTPStatusEvent;
public class HTTPStatusEventExample extends Sprite {
public function HTTPStatusEventExample() {
var loader:URLLoader = new URLLoader();
loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
var request:URLRequest = new URLRequest("http://www.[yourDomain].com/MissingFile.html");
loader.load(request);
}
private function httpStatusHandler(event:HTTPStatusEvent):void {
trace("httpStatusHandler: " + event);
trace("status: " + event.status);
}
}
}
And for your usecase (obtaining the headers):
private function httpStatusHandler(event:HTTPStatusEvent):void {
for(var i:int = 0;i<event.responseHeaders.length;i++){
trace(i.toString() + ":" + event.responseHeaders[i].name + " - " + event.responseHeaders[i].value);
}
}
I haven't tested this code, but according to your comment this approach is working.