Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C+中的libcurl+;:将URL转换为一些奇怪的符号 我注意到C++的LIGBURL改变了提供给一些奇怪符号的URL。代码如下: curl_global_init(CURL_GLOBAL_ALL); curl_handle = curl_easy_init(); cout << "http://subdomain.mydomain.com/folder/check.php?key=" + key << endl; curl_easy_setopt(curl_handle, CURLOPT_URL, "http://subdomain.mydomain.com/folder/check.php?key=" + addon_key); curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, &writeCallback); curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L); res = curl_easy_perform(curl_handle);_C++_Linux_C++11_Curl_Libcurl - Fatal编程技术网

C+中的libcurl+;:将URL转换为一些奇怪的符号 我注意到C++的LIGBURL改变了提供给一些奇怪符号的URL。代码如下: curl_global_init(CURL_GLOBAL_ALL); curl_handle = curl_easy_init(); cout << "http://subdomain.mydomain.com/folder/check.php?key=" + key << endl; curl_easy_setopt(curl_handle, CURLOPT_URL, "http://subdomain.mydomain.com/folder/check.php?key=" + addon_key); curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, &writeCallback); curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L); res = curl_easy_perform(curl_handle);

C+中的libcurl+;:将URL转换为一些奇怪的符号 我注意到C++的LIGBURL改变了提供给一些奇怪符号的URL。代码如下: curl_global_init(CURL_GLOBAL_ALL); curl_handle = curl_easy_init(); cout << "http://subdomain.mydomain.com/folder/check.php?key=" + key << endl; curl_easy_setopt(curl_handle, CURLOPT_URL, "http://subdomain.mydomain.com/folder/check.php?key=" + addon_key); curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, &writeCallback); curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L); res = curl_easy_perform(curl_handle);,c++,linux,c++11,curl,libcurl,C++,Linux,C++11,Curl,Libcurl,当我在Windows中构建项目时,这段代码工作得非常好,但当我使用Linux构建项目时,这种情况就会发生。如果我只是尝试用这段代码访问“”,它就可以工作,但只要我添加了键,libcurl就会更改整个URL 谢谢。 < P>就像我在评论中所说的,CURL库是C函数库,C函数不知道C++中的对象或类。 当您执行时http://subdomain.mydomain.com/folder/check.php?key=“+addon_key结果是一个(临时)std::string对象。将该参数传递给C函数

当我在Windows中构建项目时,这段代码工作得非常好,但当我使用Linux构建项目时,这种情况就会发生。如果我只是尝试用这段代码访问“”,它就可以工作,但只要我添加了键,libcurl就会更改整个URL


谢谢。

< P>就像我在评论中所说的,CURL库是C函数库,C函数不知道C++中的对象或类。 当您执行
时http://subdomain.mydomain.com/folder/check.php?key=“+addon_key
结果是一个(临时)
std::string
对象。将该参数传递给C函数不会很好地工作,我很惊讶编译器居然让您毫无怨言地传递该参数。我认为这应该是一个编译器错误,或者至少应该给你一个严厉的警告

您可以通过创建另一个变量来存储string对象,并使用
c_str
成员函数来获取c样式的字符串(指向常量
char
)来解决此问题:

我不知道cURL是否复制字符串,或者是否需要在完成之前保持
url
变量的活动状态


显然,它在Windows上工作只是纯粹的运气。将C++对象传递给不期望的函数是未定义的行为。< /p>什么是<代码> AddioNoKix<代码>?它是什么类型的?你知道,这个库是一个C函数库,C函数不知道C++对象(比如<代码> STD::String < /Cord>)?它是STD类型:String。看起来你的字符串没有空。terminator@deW1,谢谢你的回答。它实际上起了作用。我仍然想知道为什么它在Windows中工作,而在Linux中不工作。@Alex,我的猜测是,与GLIBC的版本相比,MSVC的
std::string
的布局有所不同。如果字符串对象的第一个
sizeof(char*)
字节恰好是指向字符串内容的指针,那么将
std::string
传递给
curl\u easy\u setopt
可能只是巧合。
curl\u easy\u setopt()
的第三个参数是可变的,因此编译器无法验证其中的输入。这就是为什么当传递一个
std::string
时代码可以编译,其中需要一个
char*
。从libcurl 7.17.0开始,
curl\u easy\u setopt()
复制
char*
input(该文件说明了这一点),因此您不需要
std::string
变量,您可以直接在临时变量上调用
c\u str()
,例如:
curl\u easy\u setopt(curl\u句柄,CURLOPT\u URL,()http://subdomain.mydomain.com/folder/check.php?key=“+addon_key).c_str());
尽管使用变量更安全
http://subdomain.mydomain.com/folder/check.php?key=tasdasm34234k23l423m4234mn23n4jk23bjk4b23nasdasdasdasdsdsd
* Rebuilt URL to: � ��g/
* IDN support not present, can't parse Unicode domains
* getaddrinfo(3) failed for �   ��g:80
* Couldn't resolve host '�  ��g'
* Closing connection 0
std::string url = "http://subdomain.mydomain.com/folder/check.php?key=" + addon_key;
curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());