Hello,
I'm trying to download a file from the server which requires basic
authentication(need to enter user name and password to access).

I came across these links:
http://stackoverflow.com/questions/509219/flex-3-how-to-support-http-authentication-urlrequest

http://johncblandii.com/2011/07/flex-quick-tip-urlrequest-basic-auth.html

http://blog.derraab.com/2010/02/25/urlrequest-with-http-authentication/

Tried all of it. I either get IO error or I get windows authentication
popup window when I run the mobile app on my desktop.

None of it seem to work. I'm using Flex4.14\AIR16.

Screenshot:
http://pbrd.co/18wmsZK


Code that I have been trying:

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009";
        xmlns:s="library://ns.adobe.com/flex/spark" title="Contact" >
    <s:layout>
        <s:VerticalLayout/>
    </s:layout>
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;
            import mx.utils.Base64Encoder;

            private var remoteURLStreamer:URLStream= new URLStream();

            private var pathRemote:String = '
http://myserver.com/datafiles/myfile.zip';

            private function startRemoteFileDownload():void
            {
                URLRequestDefaults.setLoginCredentialsForHost('
www.myserver.com','myusername','mypassword'); // not working
                remoteURLStreamer.addEventListener(ProgressEvent.PROGRESS,
remoteURLStreamerProgressHandler);
                remoteURLStreamer.addEventListener(IOErrorEvent.IO_ERROR,
remoteURLStreamerIOErrorHandler);

remoteURLStreamer.addEventListener(SecurityErrorEvent.SECURITY_ERROR,
remoteURLStreamerSecurityErrorHandler);

                var req:URLRequest = new URLRequest(pathRemote);

                req.method = URLRequestMethod.POST;

                req.data = new URLVariables("name=John+Doe"); //(one post
suggests to pass dummy parameters to data) not working

                var encoder:Base64Encoder = new Base64Encoder();
                encoder.insertNewLines = true;
                encoder.encode("myusername:mypassword");


                var credsHeader:URLRequestHeader = new
URLRequestHeader("Authorization", "Basic " + encoder.toString()); //not
working
                req.requestHeaders.push(credsHeader);
                remoteURLStreamer.load(req);

            }

            private function
remoteURLStreamerProgressHandler(event:ProgressEvent):void
            {

                switch (event.type)
                {
                    case "progress":
                        lb.text = event.bytesLoaded.toString();
                        break;
                }
            }



            private function
remoteURLStreamerIOErrorHandler(event:IOErrorEvent):void
            {
                lb.text = 'IOError';
            }

            private function
remoteURLStreamerSecurityErrorHandler(event:SecurityErrorEvent):void
            {
                lb.text = 'SecurityError';
            }


            protected function button1_clickHandler(event:MouseEvent):void
            {
                startRemoteFileDownload();
            }

        ]]>
    </fx:Script>

    <s:Button label="hello" click="button1_clickHandler(event)"/>
    <s:Label id="lb" text="Hello"/>
</s:View>



Can you kindly let me know if there is something going wrong here? Or is
there any other way to make it work?

Reply via email to