Adobe Air how to check if URL is online\gives any response exists?

后端 未结 2 1436
小鲜肉
小鲜肉 2021-01-14 23:31

I have url I want to check if it is live. I want to get bool value. How to do such thing?

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-15 00:19

    An important consideration that George's answer left out is the URLRequestMethod. If one were trying to verify the existence of rather large files (e.g, media files) and not just a webpage, you'd want to make sure to set the method property on the URLRequest to URLRequestMethod.HEAD.

    As stated in the HTTP1.1 Protocol:

    The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response.

    Hence, if you really only want to verify the existence of the URL, this is the way to go.

    For those who need the code spelled out:

    var _request:URLRequest = URLRequest(url);
    _request.method = URLRequestMethod.HEAD; // bandwidth :)
    

    Otherwise, George's answer is a good reference point.

    NB: This particular URLRequestMethod is only available in AIR.

提交回复
热议问题