【问题标题】:send http requests, keep session alive: C/C++ with cURL(axus camera)发送 http 请求,保持会话活动:C/C++ with cURL(axus camera)
【发布时间】:2013-10-09 15:50:29
【问题描述】:

我有一个问题,我完全不知道如何解决以下问题: 我想向轴相机发送多个 http 请求。 这是我的代码:

#include <stdio.h>
#include <curl/curl.h>

int main(void)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http:/root:309@IPADDRESS");
    /* example.com is redirected, so we tell libcurl to follow redirection */
//    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
    res=curl_easy_perform(curl);
    curl_easy_setopt(curl, CURLOPT_URL, "http:/IPADDRESS/axis-cgi/com/ptz.cgi?move=left");
    /* Perform the request, res will get the return code */
    res = curl_easy_perform(curl);
    /* Check for errors */
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  return 0;
}

在这个例子中,我要做的就是在登录到 IP 地址后保持会话活动,然后将命令“move=left”发送到这个 IP 地址。 当我执行这个程序时,我收到了这些消息:

<HTML>
<HEAD>
  <META HTTP-EQUIV="Expires" CONTENT="Tue, 01 Jan 1980 1:00:00 GMT"> 
  <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
  <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
    <META HTTP-EQUIV="Refresh" CONTENT="0; URL=/view/index.shtml">
Your browser has JavaScript turned off.<br>For the user interface to work effectively, you must enable JavaScript in your browser and reload/refresh this page.
  </noscript>
</HEAD>
<BODY>
</BODY>
</HTML>

<HTML><HEAD><TITLE>401 Unauthorized</TITLE></HEAD>
<BODY><H1>401 Unauthorized</H1>
Your client does not have permission to get URL /axis-cgi/com/ptz.cgi from this server.
</BODY></HTML>

我假设我什至没有登录到该 ipaddress...

我以前从来没有真正使用过这种方法......你能帮我吗?

非常感谢。

【问题讨论】:

  • 看来您必须使用curl_multi_* 功能,并启用CURLMOPT_PIPELINING。您必须使用CURLM * 注册您的CURL *。我找到了一个例子here

标签: c++ http curl request axis


【解决方案1】:

你需要添加这两个选项:

curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookie.txt");
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookie.txt");

来自文档:

CURLOPT_COOKIEFILE

将指向以零结尾的字符串的指针作为参数传递。它应该 包含保存要读取的 cookie 数据的文件的名称。饼干 数据可能是 Netscape / Mozilla cookie 数据格式或只是常规的 HTTP 样式的标头转储到文件中。

给定一个空的或不存在的文件或通过传递空字符串 (""),此选项将为此 curl 句柄启用 cookie,使其 理解并解析收到的 cookie,然后使用匹配的 cookie 未来的请求。

如果您多次使用此选项,您只需将更多文件添加到 读。后续文件将添加更多 cookie。

CURLOPT_COOKIEJAR

将文件名作为 char * 传递,以零结尾。这将使 libcurl 将所有内部已知的 cookie 写入指定文件时 curl_easy_cleanup(3) 被调用。如果不知道 cookie,则不会有任何文件 被创建。指定“-”以将 cookie 写入标准输出。 使用此选项还会为此会话启用 cookie,因此如果您 示例跟随一个位置,它将发送匹配的 cookie 相应地。

如果无法创建或写入 cookie jar 文件(当 curl_easy_cleanup(3) 被调用),libcurl 不会也不能报告 为此错误。使用 CURLOPT_VERBOSE 或 CURLOPT_DEBUGFUNCTION 将 收到要显示的警告,但这是您唯一可见的反馈 解决这个可能致命的情况

阅读更多:http://curl.haxx.se/libcurl/c/curl_easy_setopt.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-03
    • 2012-07-19
    • 2010-10-29
    相关资源
    最近更新 更多