Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
quote:
Hi Abdul,
I am looking at a similar problem with slightly different twist. It is best described by a question posted here http://192.150.14.120/cfusion/webforums/forum/messageview.cfm?catid=582&threadid=1160651&enterthread...
"I would like a standalone Flex 2.0 SWF to be able to use a set of 3rd party webservices protected by basic authentication. Is it possible? If yes, could someone put a simple step-by-step example up? I just can't see anyway to do this from the livedocs entry."
The stuff livedocs and other places seem to imply a Flex or CF server in the back-end.
What you have could be used to address this but I am not able to get it to work with the WebService object. I am using your code to do basic HTTP authentication then invoke calls using the WebService object. Though the authentication suceeds I still get prompted for user/password when I make the WebService call.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
quote:
public function sendMyWebSerice(event: MouseEvent):void
{
// Get the actual Web service from the generated WSDL class
var ws:BasePromoPlanner=promoPlannerWs.getWebService();
addAuthHeader(ws,usernameField.text,passwordField.text);
.....
...
... Send the request etc.
}
// We should have a utility function to add the credentials
private function addAuthHeader(_webservice:*, _username:String, _password:String):void
{
//add the header to service request
var encoder : Base64Encoder = new mx.utils.Base64Encoder();
encoder.encode(_username + ":" + _password);
_webservice.headers["Authorization"] = "Basic " + encoder.toString();
}
quote:
private function call(operation:WSDLOperation,args:Object,token:AsyncToken,headers:Array=null):void
quote:
message.url=endpointURI;
quote:
// We are forcing into the message the previously added headers.
message.headers["Authorization"]=this.headers["Authorization"];
message.httpHeaders["Authorization"]=this.headers["Authorization"];
Copy link to clipboard
Copied
Awesome work; solved my problem nicely.
Thanks gkohen!
Cheers,
Marc.
Copy link to clipboard
Copied
I�m am trying to convert your solution to be used in flash as a swf that wraps the flex lib swc's, but i am having some difficulties, notting is happening, no errors nothing.
package { import flash.display.Sprite; import flash.events.Event; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; import mx.rpc.http.Operation; import mx.rpc.soap.LoadEvent; import mx.rpc.soap.WebService; import mx.utils.Base64Encoder; public class Main extends Sprite { private var ws:WebService; public function Main():void { ws = new WebService(); ws.addEventListener(LoadEvent.LOAD, onLoaded); } public function addAuthHeader( userName:String, passWord:String ):void { var encoder:Base64Encoder = new Base64Encoder(); encoder.insertNewLines = false; encoder.encode(userName + ":" + passWord); ws.headers["Authorization"] = "Basic " + encoder.toString(); } public function load( url:String ):void { ws.loadWSDL(url); } private function onLoaded(evt:LoadEvent):void { trace("Loaded:", evt.wsdl); } private function wsdlFault(evt:FaultEvent):void{ trace("Fault:", evt.fault); } private function wsdlResult(evt:ResultEvent):void { trace("Resault:", evt.result); } } }
Copy link to clipboard
Copied
I know this is five years later on this forum but there's no solution here or on any other forum. So after two day's of hammering this out I was able to produce a workable solution. Create a new user account for testing.
Wu_Xiao's explanation of the issue was dead on. The WebService does a GET then a POST and for the GET we are unable to supply the Authorization in the header and this is why we get the popup. The POST has the Authorization but it already to late.
Take these actions
1 Create a new user on the machine with the web services. Start with a simple name and password (text only) I had issues with different users. Start clean and simple.
2 Copy your web services a second usable service. You'll see in my example below i have Ive copied ServicesSECURE/Services1.asmx?WSDL to ServicesDEFINITION/Services1.asmx?WSDL
3 Remove all of the code inside of your services making the new set of service like a definition service. Make sure all of the inputs and outputs are the same. ServicesDEFINITION will have no coding and empty returns.
Example
[WebMethod]
public String CountUsers(string group)
{
return "";
}
4 Implement the code below. Call the init() right away to instantiate the web service and wait until it loads to use any of the services. I use a button event to test.
private var testws:WebService = new WebService;
private function init():void
{
testws.wsdl="http://test.com/ServicesDEFINITION/Services1.asmx?WSDL";
var encoder:Base64Encoder= new Base64Encoder();
encoder.insertNewLines = false; // see below for why you need to do this
encoder.encode("USERNAME:PASSWORD");
testws.httpHeaders = {Authorization:" Basic " + encoder.toString()};
testws.loadWSDL();
testws.addEventListener("load", wsdlLoadHandler);
}
protected function test_clickHandler(event:MouseEvent):void
{
testws["CountUsers"].addEventListener(mx.rpc.events.FaultEvent.FAULT,testFaultHandler);
testws["CountUsers"].addEventListener(mx.rpc.events.ResultEvent.RESULT,testResultHandler);
testws.endpointURI="http://test.com/ServicesSECURE/Services1.asmx?WSDL";
testws.getOperation("CountUsers").send("Test");
}
protected function wsdlLoadHandler (event:LoadEvent) : void
{
//the service has to load before using the getOperation function
//you could try using mx.core.UIComponent.callLater from
//this listener and call the gettestws.getOperation("Co....
}
So you'll see that the ServicesDEFINITION (GET) is called and grabs the definition of the Service1 service but this unsecured services is useless because we've removed all of the code. After the definition GET is called we can change the end point using endpointURI and perform the POST against our secure ServicesSECURE.
Copy link to clipboard
Copied
Find more inspiration, events, and resources on the new Adobe Community
Explore Now