14 Replies Latest reply on Mar 28, 2012 11:23 PM by Praful_Comp

    How to call web service with basic authentication

    Praful_Comp

      Hi ,

      i want to call web service which has basic authentication

      Also along with web service , i  want to pass 2 parameters to web service function which will return some object.

       

      can anybody help me plz?

        • 1. Re: How to call web service with basic authentication
          Patel Bharat Level 2

          Hi, try code as below:

          <mx:Script>

           

          <![CDATA[

                     import mx.rpc.soap.WebService;

                    import mx.rpc.soap.SOAPFault;            

                    import mx.rpc.events.ResultEvent;

                    import mx.rpc.events.FaultEvent;

           

                     private var ws:WebService;

           

                     private function get_result(evt:ResultEvent):void {

                          // do code here

                      }

           

                    

                     private function get_fault(evt:FaultEvent):void {

                          // error code here           

                      }

           

                    private function button_click():void {

                             

                          ws = new WebService();

                          ws.wsdl= "web service path";

                          ws.validateUser.addEventListener("result", get_result);

                          ws.addEventListener("fault", get_fault);

                          ws.loadWSDL();

                          ws.validateUser(userName, password);

                         

                       }

           

           

          ]]>

          </mx:Script>

           


          <mx:Button id="button"

                            label="validate user from web service"

                            click="button_click();" />

           

          1 person found this helpful
          • 2. Re: How to call web service with basic authentication
            Praful_Comp Level 1

            Thanks for reply.

             

             

            But  this code is not working .

             

            I also tried validation using

             

                 ws.setCredentials("username","password");

                 ws.setRemoteCredentials("username" , "password" )

             

            these are also get failed

             

             

             

             

             

            • 3. Re: How to call web service with basic authentication
              Patel Bharat Level 2

              For more details, Can you share the code snippets here?

              • 4. Re: How to call web service with basic authentication
                Praful_Comp Level 1

                <fx:Declarations>
                 
                  <s:WebService  id="service"
                   wsdl="web service url" useProxy="false">

                 

                <!-- here login_user is the function in web service which takes 2 parameter as Email and password  and return object as a result which is taken in web_resultHandler(event)-->

                 

                 

                   <s:operation name="login_user" resultFormat="xml" result="web_resultHandler(event)" >
                 

                               <s:request>

                                         <Email>{Email.text}</Email>

                                         <Password>{Password.text}</Password>

                              </s:request>

                  

                   </s:operation>

                </s:WebService>

                </fx:Declaration>

                 

                <fx:Script>

                  <![CDATA[

                   import mx.rpc.events.ResultEvent;

                   import mx.utils.Base64Encoder;

                  

                   private function authAndSend(service:WebService):void    

                   {      

                 

                          service.setRemoteCredentials('username', 'password');       

                         service.send();

                   }


                   protected function web_resultHandler(event:ResultEvent):void
                   {
                    // TODO Auto-generated method stub
                     var p:Object = event.result;
                   }

                 

                ]]>

                 

                 

                 

                 

                </fx:Script>

                <s:Button  label="Sign In"     click="authAndSend(service)"    />

                 

                • 5. Re: How to call web service with basic authentication
                  Patel Bharat Level 2

                  <s:operation> name is the name of the WebService method. Here we have 'login_user but when it's call ?

                   

                  Refer the link it would me more helpful for that:

                   

                  http://help.adobe.com/en_US/Flex/4.0/AccessingData/WS2db454920e96a9e51e63e3d11c0bf69084-7f db.html

                  • 6. Re: How to call web service with basic authentication
                    Praful_Comp Level 1

                    thanks .

                     

                    But  <s:operation>   can be only used in <webService>    

                    I need equivalent component in   <HTTPService>

                    • 7. Re: How to call web service with basic authentication
                      Patel Bharat Level 2

                      You can use as below:

                       

                      private function send_data():void

                      {

                                           userRequest.send();

                      }


                       

                      <mx:HTTPService id="userRequest" url="http://localhost/myproj/request_post2.php" useProxy="false" method="POST">

                                   <mx:request xmlns="">

                                            <username>{username.text}</username>

                                            <emailaddress>{emailaddress.text}</emailaddress>

                                   </mx:request>

                      </mx:HTTPService>

                      • 8. Re: How to call web service with basic authentication
                        Praful_Comp Level 1

                        Hi ,

                         

                             authAndSend( ) is not the function defined in web service , I am using this function for web service validation .

                        there is function xyz( ) defined in web service , that i need to call through following code , how to do that ?

                        please help.

                            

                         

                         

                        <fx:Script>
                          <![CDATA[
                           import mx.rpc.events.FaultEvent;
                           import mx.rpc.events.ResultEvent;
                           import mx.rpc.soap.Operation;
                           import mx.rpc.soap.SOAPFault;
                           import mx.utils.Base64Encoder;
                           private var getdata:Operation;
                          

                        //function for web service validation
                           private function authAndSend(service:HTTPService):void    
                           {     
                            var encoder:Base64Encoder = new Base64Encoder();            
                            encoder.insertNewLines = false;            
                            encoder.encode("username:password"); 
                            service.headers = {Authorization:"Basic " + encoder.toString()};
                            service.send();
                               
                           }
                          

                          
                           
                           private function resultHandler(event:ResultEvent):void        
                           {
                          

                         

                               var webresult:String=event.result.toString();
                           
                           }
                          
                           private function resultFailed(event:FaultEvent):void
                           {
                            var err:String = event.fault.toString(); 
                           
                           }
                          
                          
                           
                          ]]>
                         
                        </fx:Script>

                         


                        <fx:Declarations>
                         
                          <s:HTTPService id="service" url="web service url"
                                resultFormat="xml" result="resultHandler(event)">
                          
                           
                          </s:HTTPService>
                         
                          </fx:Declarations>

                         

                        <s:Button id="button"

                            

                             label="call service"

                            

                             click="authAndSend(service)" />

                        • 9. Re: How to call web service with basic authentication
                          drkstr_1 Level 4

                          To call a specific service method (IE. a SOAP service), you need to use WebService, not HTTPService. HTTPService is for simple GET and POST transactions, where the entire output buffer is read in as the result (usually as a String).

                           

                          Both WebService and HTTPService use HTTP headers to control basic authentication, however, I believe the property is called 'httpHeaders' for a WebService.

                          • 10. Re: How to call web service with basic authentication
                            Praful_Comp Level 1

                            Thanks for reply .

                             

                                 I have also use <webService > component  But authentication is not working .

                             

                             

                            this is my code

                             

                             

                            <?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="webservicelattest">

                             

                             

                            <fx:Script>

                              <![CDATA[

                               import mx.rpc.events.ResultEvent;

                               import mx.utils.Base64Encoder;

                              

                               private function authAndSend(service:WebService):void    

                               {      

                               

                                var encoder:Base64Encoder = new Base64Encoder();            

                                encoder.insertNewLines = false;            

                                encoder.encode("username:password"); 

                                service.httpHeaders = {Authorization:"Basic " + encoder.toString()}; 

                                  service.initialize();

                                //service.loadWSDL();

                               // service.send();

                               

                               

                                  }

                              

                              

                              

                               protected function web_resultHandler(event:ResultEvent):void

                               {

                                // TODO Auto-generated method stub

                                 var webresult:String=event.result.toString();

                               }

                              

                              

                              

                              

                              

                              ]]>

                             

                             

                             

                             

                             

                            </fx:Script>

                             

                             

                              <fx:Declarations>

                              

                              

                               <s:WebService

                                id="service"

                                wsdl="web service urll" useProxy="false">

                               

                                <s:operation name="xyz" resultFormat="xml" result="web_resultHandler(event)" >

                               

                                <s:request>

                                <Email>{"email"}</Email>

                                <Password>{"password"}</Password>

                                </s:request>

                               

                                </s:operation>

                                  

                               </s:WebService>

                              

                               <!-- Place non-visual elements (e.g., services, value objects) here -->

                              </fx:Declarations>

                             

                               <s:Button  label="call web service"    click="authAndSend(service)"    />

                             

                            </s:View>

                             

                             

                             

                             

                             

                            it is showing me error as

                             

                             

                             

                             

                            [RPC Fault faultString="Could not load WSDL" faultCode="Server.NoServicesInWSDL" faultDetail="No <wsdl:service> elements found in WSDL at ."]

                            at mx.rpc.wsdl::WSDL/getService()[E:\dev\4.y\frameworks\projects\rpc\src\mx\rpc\wsdl\WSDL.as :278]

                            at mx.rpc.wsdl::WSDL/getPort()[E:\dev\4.y\frameworks\projects\rpc\src\mx\rpc\wsdl\WSDL.as:19 9]

                            at mx.rpc.soap::WebService/http://www.adobe.com/2006/flex/mx/internal::wsdlHandler()[E:\dev\4.y\frameworks\projects\rpc\src\mx\rpc\soap\WebService.as:310]

                            at flash.events::EventDispatcher/dispatchEventFunction()

                            at flash.events::EventDispatcher/dispatchEvent()

                            at mx.rpc.wsdl::WSDLLoader/checkLoadsOutstanding()[E:\dev\4.y\frameworks\projects\rpc\src\mx \rpc\wsdl\WSDLLoader.as:203]

                            at mx.rpc.wsdl::WSDLLoader/resultHandler()[E:\dev\4.y\frameworks\projects\rpc\src\mx\rpc\wsd l\WSDLLoader.as:171]

                            at flash.events::EventDispatcher/dispatchEventFunction()

                            at flash.events::EventDispatcher/dispatchEvent()

                            at HTTPOperation/http://www.adobe.com/2006/flex/mx/internal::dispatchRpcEvent()[E:\dev\4.y\frameworks\projects\rpc\src\mx\rpc\http\HTTPService.as:993]

                            at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::resultHandler()[E:\dev\4.y\frameworks\projects\rpc\src\mx\rpc\AbstractInvoker.as:318]

                            at mx.rpc::Responder/result()[E:\dev\4.y\frameworks\projects\rpc\src\mx\rpc\Responder.as:56]

                            at mx.rpc::AsyncRequest/acknowledge()[E:\dev\4.y\frameworks\projects\rpc\src\mx\rpc\AsyncReq uest.as:84]

                            at DirectHTTPMessageResponder/completeHandler()[E:\dev\4.y\frameworks\projects\rpc\src\mx\me ssaging\channels\DirectHTTPChannel.as:451]

                            at flash.events::EventDispatcher/dispatchEventFunction()

                            at flash.events::EventDispatcher/dispatchEvent()

                            at flash.net::URLLoader/onComplete()

                            • 11. Re: How to call web service with basic authentication
                              Patel Bharat Level 2

                              What you are passing in WSDL ?

                              • 13. Re: How to call web service with basic authentication
                                Patel Bharat Level 2

                                there should be webservice URL path for wsdl, for example like:

                                 

                                 

                                wsdl="http://www.abc.com/Flexwebservice.asmx?wsdl"


                                • 14. Re: How to call web service with basic authentication
                                  Praful_Comp Level 1

                                  ya using the same , still not getting validated

                                   

                                   

                                   

                                  please help me to solve the problem .