如何从libcurl获取URL的片段部分? [英] How to get the fragment part of an URL from libcurl?
问题描述
我被重定向到地址如http://example.com#foo=bar
的页面.我想得到它的foo=bar
部分.整个事情也可以.
I get redirected to a page with address like http://example.com#foo=bar
. I want to get foo=bar
part of it. The whole thing would be ok too.
我发现了这个东西:
char * url;
curl_easy_getinfo(myHandle, CURLINFO_EFFECTIVE_URL, &url);
我不太懂英语,无法自己找到信息.每次我想查找它时,都会找到有关将页面放入字符串变量的信息.
I don't know english well to find information myself. Every time I want to find it, I find information on getting the page into string variable.
代码:
std::string readBuffer;
curl_global_init( CURL_GLOBAL_ALL);
CURL * myHandle;
CURLcode result;
myHandle = curl_easy_init();
curl_easy_setopt(myHandle, CURLOPT_COOKIEJAR, "coo.txt");
curl_easy_setopt(myHandle, CURLOPT_COOKIEFILE, "coo.txt");
curl_easy_setopt(myHandle, CURLOPT_URL, "https://www.google.ru/#q=stack");
curl_easy_setopt(myHandle, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(myHandle, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_setopt(myHandle, CURLOPT_FOLLOWLOCATION, 1L);
result = curl_easy_perform(myHandle);
char * ch_cur_url;
result = curl_easy_getinfo(myHandle, CURLINFO_EFFECTIVE_URL,
&ch_cur_url);
printf("%s\n", ch_cur_url);
输出https://www.google.ru/
当我想要https://www.google.ru/#q=stack
推荐答案
cURL根据错误报告( 2 ).另请参见此修补程序.因此,片段标识符"不能作为CURLINFO_EFFECTIVE_URL
的一部分使用.
cURL removes the "fragment identifier" from the URL before making a request, as per the bug reports (1, 2). See also this patch. Thus the "fragment identifier" is not available as part of the CURLINFO_EFFECTIVE_URL
.
如果片段标识符"作为重定向的一部分返回(例如,Location
HTTP标头),而您无法通过其他任何方式获取它,则可以使用调试模式来窥视之间的通信. cURL和服务器,然后自己提取片段标识符".为此,您需要设置CURLOPT_DEBUGFUNCTION
或CURLOPT_HEADERFUNCTION
.
If the "fragment identifier" is returned as part of a redirect (e.g. the Location
HTTP header) and you can't get it any other way, then you may use the debug modes to peek on the communications between the cURL and the servers and extract the "fragment identifier" yourself. To that end you'll need to setup either CURLOPT_DEBUGFUNCTION
or CURLOPT_HEADERFUNCTION
.
P.S.一点建议:查阅相关信息非常容易.我做的第一件事是学习#foo=bar
的正式"名称.为了得到它,我在 URL 上访问了Wikipedia,并被带到
P.S. A bit of advise: Googling the relevant information was very easy. First thing I did was to learn the "official" name of the #foo=bar
. To get it I visited Wikipedia at URL and was brought to Fragment identifier. After that, Googling with the "curl fragment" netted the relevant parts. If you're looking for something, learn it's proper name.
这篇关于如何从libcurl获取URL的片段部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!