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

使用CUPS C API发送IPP请求时出现Service Unavailable错误

问题分析与解决方案

你的代码出现SERVICE_UNAVAILABLE(1282)错误,核心原因是IPP操作码、打印机URI及请求资源路径的匹配问题,以下是具体排查和修正方案:

关键错误点

  1. 误用IPP操作码
    CUPS_GET_PRINTERS是用于获取所有打印机列表的请求,此时不需要指定单个打印机的printer-uri,且请求资源路径应为/printers/。如果目标是获取单台打印机的属性,应改用IPP_OP_GET_PRINTER_ATTRIBUTES(对应CUPS标准操作码)。

  2. 打印机URI不正确
    你使用的Bonjour格式URI(ipp://EPSON%20XP-3150%20Series%20(USB)._ipp._tcp.local/)不是CUPS本地管理的队列URI,CUPS本地打印机队列的标准格式为ipp://localhost/printers/[队列名],即你lpstat命令中用到的EPSON_XP_3150_Series_USB_,正确URI应为ipp://localhost/printers/EPSON_XP_3150_Series_USB_。

  3. resource参数设置错误
    cupsDoRequest的第三个参数resource是HTTP请求的资源路径,而非完整IPP URI:

    • 获取单台打印机属性时,应设为"/printers/EPSON_XP_3150_Series_USB_"
    • 获取所有打印机列表时,应设为"/printers/"

修正后的代码示例

#include <cups/cups.h>
#include <cups/ipp.h>

int main() {
    // 定义需要获取的打印机属性列表
    const char *requested_attributes[] = {
        "printer-state",
        "printer-state-message",
        "printer-uri-supported",
        "printer-name"
    };

    // 使用正确的操作码获取单台打印机属性
    ipp_t *request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
    if (!request) {
        printf("Failed to create IPP request\n");
        return 1;
    }

    // 添加本地CUPS打印机URI
    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, 
                 "ipp://localhost/printers/EPSON_XP_3150_Series_USB_");

    // 添加请求的属性字段
    ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes", 
                  sizeof(requested_attributes)/sizeof(requested_attributes[0]), 
                  NULL, requested_attributes);

    // 添加请求用户名
    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL, cupsUser());

    // 设置正确的资源路径
    const char *resource = "/printers/EPSON_XP_3150_Series_USB_";

    // 发送IPP请求
    ipp_t *response = cupsDoRequest(CUPS_HTTP_DEFAULT, request, resource);

    // 处理响应结果
    if (!response) {
        printf("%d - %s\n", cupsLastError(), cupsLastErrorString());
    } else {
        // 遍历并打印属性
        ipp_attribute_t *attr;
        for (attr = ippFirstAttribute(response); attr != NULL; attr = ippNextAttribute(response)) {
            printf("%s: %s\n", ippGetName(attr), ippGetString(attr, 0, NULL));
        }
        ippDelete(response);
    }

    return 0;
}

额外排查建议

  • 确保编译时链接CUPS库:gcc your_code.c -o printer_info -lcups
  • 若问题仍存在,可开启CUPS调试日志:修改/etc/cups/cupsd.conf的LogLevel为debug,重启CUPS服务后查看/var/log/cups/error_log,获取请求的详细错误信息。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 12:15:32