【问题标题】:Dynamic URL with curl C++使用 curl C++ 的动态 URL
【发布时间】:2018-10-31 09:05:07
【问题描述】:

这是我用于 curl GET 查询的 C++ 代码(在 QT5 中)

CURL *curl;
CURLcode res;
curl = curl_easy_init();

curl_easy_setopt(curl, CURLOPT_URL, "http://localhost/?site=http://google.fr&name=test");
curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/7.42.0");

此代码完美运行!请求已启动。

我想更改应用程序字段的路径。
我通过这个变量得到域

QString domain = ui->editDomain->text();

我试过了:

curl_easy_setopt(curl, CURLOPT_URL, domain + "?site=http://google.fr&name=test");

但是这里的查询没有启动!而且我不明白为什么... 并且编译时没有错误

【问题讨论】:

  • 域名的内容是什么?
  • @user3606329 检索 GET 变量并将其写入文件的 php 文件。但它适用于硬编码的 url!
  • 确保您在将网址放在一起时没有错过/domain.com?site=.. 无效。这就是我要求提供内容的原因。
  • 仅供参考,Qt 中已经内置了网络功能,除非您需要一些深奥的功能,否则无需使用 Curl。

标签: c++ qt curl


【解决方案1】:

curl 函数采用 c 字符串。 domain + "?site=http://google.fr&name=test" 给你一个 QString 函数不知道如何处理。您需要做的是将生成的 QString 转换为 c 字符串。你可以这样做

curl_easy_setopt(curl, CURLOPT_URL, (domain + "?site=http://google.fr&name=test").toUtf8().constData());

【讨论】:

  • 哦!很有趣,而且效果很好 =) 谢谢
  • 第三个参数并不总是c字符串!需要作为第三个参数传递给curl_easy_setopt 的类型取决于作为第二个参数传递的选项。阅读docs了解详情。
猜你喜欢
  • 1970-01-01
  • 2011-02-03
  • 1970-01-01
  • 2018-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-26
相关资源
最近更新 更多