After all the experiments, this is what I have got.
Om, as3httpclient isn't giving me a progress event(I need progress event to
write the downloading file data in chunks). I thought onData event would
give me progress, but it isn't. Is there any other event I can try?
Kevin,
Your solution worked well. It authenticated the URL file(zip file). I did
not get username\password popup. It writes the file and creates it. But it
isn't opening the zip file. When I looked at the file size, I could see
320bytes of data is more than original file size. I think that's why file
is reported as corrupt and it is not opening.
This is what I am doing under progress event to create the zip file(it just
appends the data that we get in progress)
private function socket_dataHandler(event:ProgressEvent):void
{
//the tricky part, figuring out what to do with the raw data
// trace(socket.readUTFBytes(event.bytesLoaded));
localZipFile =
File.desktopDirectory.resolvePath('C:\\Desktop\\dowloadedZipFile.zip');
var bytes:ByteArray = new ByteArray();
socket.readBytes(bytes,bytes.length);
fileStream = new FileStream();
fileStream.addEventListener(Event.CLOSE, fileClosed);
fileStream.addEventListener(IOErrorEvent.IO_ERROR, onFSIOError);
fileStream.open( localZipFile, localZipFile.exists ?
FileMode.APPEND:FileMode.WRITE);
fileStream.writeBytes( bytes );
fileStream.close();
}
After that, I tried to download and write a simple text file instead of zip
file. The text file just has 'Hello' written in it. This time, file got
downloaded and got written on the disk. But when I opened it, this is what
I got:
"HTTP/1.1 200 OK
Cache-Control: max-age=31536000
Content-Type: text/plain
Last-Modified: Wed, 01 Apr 2015 05:20:10 GMT
Accept-Ranges: bytes
ETag: "859b56863b6cd01:0"
Server: Microsoft-IIS/7.0
X-Powered-By: ASP.NET
Date: Wed, 01 Apr 2015 05:55:05 GMT
Connection: close
Content-Length: 5
Hello"
And I feel this is the reason, zip is getting corrupted as it has some
extra header details in it.
So how can I avoid writing this header part to my zip file?
On Sat, Mar 28, 2015 at 8:25 AM, kevin.godell <[email protected]>
wrote:
> I duplicated your situation with basic authentication and had the same
> trouble of not being able to include the Authentication header using
> urlstream.
> Using Socket will allow you to authenticate and access the protected file,
> but is a bit trickier.
>
> private var socket:Socket;
>
> private function connectSocket():void
> {
> if (!socket)
> socket = new Socket;
> socket.addEventListener(Event.CONNECT,
> socket_connectHandler);//socket successfully connected
> socket.addEventListener(IOErrorEvent.IO_ERROR,
> socket_IOErrorHandler);//socket failed to connect
> socket.addEventListener(ProgressEvent.SOCKET_DATA,
> socket_dataHandler);//received data from socket
> socket.addEventListener(Event.CLOSE, onSocketClose);//socket
> closed by
> server
> }
> socket.connect("website.com", 80);
> }
>
> private function socket_connectHandler(event:Event):void
> {
> var b64:Base64Encoder = new Base64Encoder;
> b64.encode("user1:user1Password");
> var httpRequest:String = "GET /protected/testfile.txt
> HTTP/1.1\r\n";//might try 1.0 depending on situation
> httpRequest += "Authorization: Basic " + b64.toString() + "\r\n\r\n";
> socket.writeUTFBytes(httpRequest);
> sock.flush();
> }
>
> private function socket_dataHandler(event:ProgressEvent):void
> {
> //the tricky part, figuring out what to do with the raw data
> trace(socket.readUTFBytes(event.bytesLoaded));
> }
>
>
>
>
>
>
> -----
> .
> --
> View this message in context:
> http://apache-flex-users.2333346.n4.nabble.com/HTTP-Basic-Authentication-for-URLRequest-tp9803p9927.html
> Sent from the Apache Flex Users mailing list archive at Nabble.com.
>