You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为AppleScript中NSURLConnection的同步请求设置超时?

为AppleScript的HTTP检测函数添加超时参数

问题描述

我编写了一个AppleScript函数,接收API端点的URL,根据HTTP GET请求是否有响应返回true或false:

use AppleScript version "2.4"
use framework "Foundation"

on ping(api_url)
    -- Create the request object.
    set ca to current application
    set url_string to ca's |NSURL|'s URLWithString:api_url
    set request to ca's |NSURLRequest|'s requestWithURL:url_string
    
    -- Try to connect to the endpoint.
    set responseData to missing value
    set status to missing value
    
    set _response to ca's NSHTTPURLResponse's alloc()'s init()
    set {_data, _error} to ca's NSURLConnection's sendSynchronousRequest:request ¬
        returningResponse:_response |error|:(reference)
    
    if _error is missing value then
        -- No error => connected.
        return true
    else if class of _error is ca's NSURLError then
        return false
    else
        -- I'm unsure this can ever happen, but let's play it safe.
        return false
    end if
end ping

-- Simple test.

if ping("https://google.com") then
    log "yes"
else
    log "no"
end if

现在想添加一个超时参数,若在指定时间内无响应则返回false。尝试为NSURLConnection或NSURLRequest添加timeoutInterval时出现“未知选择器”错误,尝试的代码如下:

set {_data, _error} to ca's NSURLConnection's sendSynchronousRequest:request ¬
    timeoutInterval:5 returningResponse:_response |error|:(reference)
set {_data, _error} to ca's NSURLConnection's sendSynchronousRequest:request ¬
    returningResponse:_response |error|:(reference) ¬ 
    cachePolicy:(ca's NSURLRequestReloadIgnoringCacheData) ¬
    timeoutInterval:5

错误信息:

+[NSURLConnection
sendSynchronousRequest:timeoutInterval:returningResponse:error:]:
unrecognized selector sent to class 0x1eee55848

解决方案

问题出在超时参数的设置位置错误:NSURLConnection的同步请求方法并没有直接接收timeoutInterval的选择器,超时时间需要在NSURLRequest对象初始化时配置,而不是在发送请求时传递。

修改后的完整代码如下:

use AppleScript version "2.4"
use framework "Foundation"

-- 添加可选的timeout参数,默认5秒
on ping(api_url, timeout)
    -- 如果未传入timeout参数,使用默认值5
    if timeout is missing value then set timeout to 5
    
    set ca to current application
    set url_string to ca's |NSURL|'s URLWithString:api_url
    -- 使用带超时的请求初始化方法:requestWithURL:cachePolicy:timeoutInterval:
    set request to ca's |NSURLRequest|'s requestWithURL:url_string ¬
        cachePolicy:(ca's NSURLRequestReloadIgnoringCacheData) ¬
        timeoutInterval:timeout
    
    set _response to ca's NSHTTPURLResponse's alloc()'s init()
    set {_data, _error} to ca's NSURLConnection's sendSynchronousRequest:request ¬
        returningResponse:_response |error|:(reference)
    
    if _error is missing value then
        return true
    else if class of _error is ca's NSURLError then
        return false
    else
        return false
    end if
end ping

-- 测试示例:使用3秒超时检测google.com
if ping("https://google.com", 3) then
    log "连接成功"
else
    log "连接失败(超时或无法访问)"
end if

关键修改点

  • 为ping函数添加了可选的timeout参数,默认值设为5秒,方便灵活调用
  • 创建NSURLRequest时,使用requestWithURL:cachePolicy:timeoutInterval:方法,直接指定超时时间和缓存策略(这里选择忽略缓存,确保每次请求都是实时检测)
  • 保持NSURLConnection的同步请求调用不变,因为超时规则已经在请求对象中定义,请求会在指定时间内无响应时自动返回超时错误,最终函数返回false

内容的提问来源于stack exchange,提问作者mhucka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 18:01:04