Created
May 16, 2011 12:21
-
-
Save peterblazejewicz/974346 to your computer and use it in GitHub Desktop.
send HEAD request with flash.net.Socket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| protected function sendRequestHandler(event:MouseEvent):void | |
| { | |
| var request:URLRequest = new URLRequest("http://www.multidmedia.com/downloads/exchange/files/Simple_Applescript_Sample_042011.zip"); | |
| sendHeadRequest(request); | |
| request = null; | |
| } | |
| // | |
| private var socket:Socket = null; | |
| private var httpRequest:String = null; | |
| private var httpServer:String = null; | |
| protected function sendHeadRequest(request:URLRequest):void | |
| { | |
| debug("send HEAD request to: "+request.url); | |
| var tokens:Array = request.url.split("http://")[1].split("/"); | |
| if(tokens.length > 0) | |
| { | |
| httpServer = tokens.shift() as String; | |
| httpRequest = "/"+tokens.join("/"); | |
| }; | |
| socket = new Socket(httpServer, 80); | |
| socket.addEventListener(Event.CONNECT, connectHandler); | |
| socket.addEventListener(Event.CLOSE, closeHandler); | |
| socket.addEventListener(IOErrorEvent.IO_ERROR, errorHandler); | |
| socket.addEventListener(ProgressEvent.SOCKET_DATA, progressHandler); | |
| }; | |
| // | |
| private function connectHandler(event:Event):void | |
| { | |
| debug("connectHandler"); | |
| // sends request/headers | |
| if(socket && socket.connected) | |
| { | |
| var payload:String = "HEAD "+httpRequest+" HTTP/1.0\r\n"; | |
| payload += "Host: "+httpServer+"\r\n"; | |
| socket.writeUTFBytes(payload+"\r\n\r\n"); | |
| socket.flush(); | |
| debug("HEAD request sent"); | |
| } | |
| }; | |
| protected function closeHandler(event:Event):void | |
| { | |
| debug("close event"); | |
| socket.removeEventListener(Event.CONNECT, connectHandler); | |
| socket.removeEventListener(Event.CLOSE, closeHandler); | |
| socket.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler); | |
| socket.removeEventListener(ProgressEvent.SOCKET_DATA, progressHandler); | |
| }; | |
| protected function errorHandler(event:IOErrorEvent):void | |
| { | |
| debug("error: "+event.errorID); | |
| if(socket.connected) socket.close(); | |
| }; | |
| protected function progressHandler(event:ProgressEvent):void | |
| { | |
| var dataStr:String = null; | |
| try{ | |
| dataStr = socket.readUTFBytes(socket.bytesAvailable); | |
| } catch (error:Error) | |
| { | |
| debug("error: "+error.message); | |
| }; | |
| debug("data received:"); | |
| debug(dataStr); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment