views:

2058

answers:

4

I have a flex app, where the user can provide a link to a resource. I want to perform a simple validation and check if the url is actually valid (not just structure, but actually test the link to ensure I get a 200 OK).

I figured I would just use HttpService and use the HEAD method, but it seems that this is only available when you set useProxy to true, but I tried that and I still get errors; so I am pretty sure I am doing something wrong....here is a snippet of the code

var service:HTTPService = new HTTPService();
service.method = "HEAD"; 
service.url = url;
service.useProxy = true;
service.addEventListener(ResultEvent.RESULT, result);       
service.addEventListener(FaultEvent.FAULT, error);       
service.send();

Any idea what I am doing wrong? I am also open to other suggestions as to how to check if a url is valid (I would prefer to do this directly from Flex, without having to go back & forth to the server). Thanks.

EDIT (8/13/2009)

I implemented a simple UrlValidator by using a UrlLoader and assigning 2 listeners on it. one for IOError, and the other for Progress. My thinking was that Progress would act similar to a HEAD call, and I could just kill the stream after some amount of data was received. Unfortunately, the progress event is called in the case of a 404 or 403, which defeats the purpose. I also tried this with the Open event, but got the same results. Any ideas?

A: 

hello,

  service.method = "HEAD";

should be "POST" or "GET" (default is "GET") but not "HEAD"

service.useProxy = true;

not necessary, remove the line...

if the URL is reachable you will get a RESULT event.

OXMO456
The issue with using a GET request is that it will actually retrieve the entire file (or attempt to). This is not desired as the file may be large. I simply want to check if it exists.
gmoniey
well...I think you could use URLLoader instead of HTTPService and listen to the OPEN event and all others events dispatched by URLLoaderYou can stop the loading when you get the OPEN event using the close() method
OXMO456
A: 

Every time I try to run your code with various servers, I'm getting policy errors, so it might be a good idea to use Security.loadPolicyFile(url); first before trying to run this code? I'm getting thrown out of the office, but I'll try to look at this again when I get home:)

quoo
Actually, I believe this doesn't work because my swf is nested within a browser, and browsers don't really support methods other than GET/POST
gmoniey
What do you mean by 'this'? Security.loadPolicyFile works fine within the browser.
quoo
I'm not 100% sure here, but I believe that the requests originating from the SWF go through the browser, so they are restricted by the browsers limitations. Such limitations are not being able to actually perform PUT/DELETE requests. Hence the reason for hacks when performing REST actions from within browser.
gmoniey
Ohh. yeah, then I'd try the URLLoader.load() and then stop once you hit the httpResponseStatus event, which I know isn't ideal, but would work... or I still suspect if you call Security.loadPolicyFile(http://servertotest.com/fullurl) would also give you the information you're looking for. Sorry I wasn't more help!
quoo
A: 

Adobe's Flex docs state: "When you do not go through the server-based proxy service, you can use only HTTP GET or POST methods. However, when you set the useProxy property to true and you use the server-based proxy service, you can also use the HTTP HEAD, OPTIONS, TRACE, and DELETE methods."

The server-based proxy service is referring to Live Cycle Data Services

I know AIR supports HEAD requests using the ServiceMonitor, but it looks like Flex on it's own doesn't.

Having a quick search around, it looks a few people have used sockets to roll their own custom URLLoader classes which can access the request headers - you could try one of them

misterduck
A: 

HEAD is only supported if service.useProxy is set to true. But service.useProxy refers to the HTTPProxyService in BlazeDS/LCDS. If you are creating a client application only, you will need to set service.useProxy to false and use either GET or POST.

Another problem, is that if you are bypassing the HTTPProxyService, at least one of the following must be true:

  • The URL must in the same domain as your Flex application.
  • A crossdomain.xml (cross-domain policy) file must be installed on the web server hosting the document that allows access from the domain of the application.
Martin Harrigan
Thats what I figured. Its unfortunate that you can't at least perform GETs on sites that don't have a crossdomain. Or allow your app to bypass the security by signing it (or something similar to what you can do with java applets)
gmoniey