当前位置:网站首页>Libcurl redirection
Libcurl redirection
2022-07-23 05:33:00 【CAir2】
libcurl Redirect url:
301 Permanent redirection: The requested page has been permanently moved to a new location . When the server returns this response , The requester is automatically moved to a new location . The search engine will turn the included links into redirected links , The weight will also be transferred to the new link .302 Temporary redirection: The server is currently responding to requests from pages in different locations , But the requester should continue to use the original location for future requests . The search engine will continue to display the included links .
Get redirection url There are two ways :
Method 1 : adopt Header Of Location: obtain
size_t HeaderCallback4Download(char *ptr,size_t size,size_t nmemb,void *userdata)
{
// adopt Location: Get redirection url
CurlDownloadContext* dld_ctx = (CurlDownloadContext*)userdata;
const char key_loc[] = "Location:";
if (_strnicmp(ptr, key_loc, strlen(key_loc)) == 0)
{
dld_ctx->redirect_url.assign(ptr + strlen(key_loc));
// Get rid of \r\n
if (dld_ctx->redirect_url.size() > 2)
{
dld_ctx->redirect_url.resize(dld_ctx->redirect_url.size() - 2);
}
}
return size * nmemb;
}
// Set header callback
::curl_easy_setopt(pcurl, CURLOPT_HEADERFUNCTION, HeaderCallback4Download);
::curl_easy_setopt(pcurl, CURLOPT_HEADERDATA, &dld_ctx);
Method 2 : adopt CURLINFO_REDIRECT_URL obtain
CURLcode ret = ::curl_easy_perform(pcurl);
// Get the status code and redirection after execution url
if(ret == CURLE_OK)
{
long httpcode;
curl_easy_getinfo(pcurl, CURLINFO_RESPONSE_CODE, &httpcode);
if (httpcode == 301 || httpcode == 302)
{
char *redirect_url = NULL;
//1 Indicates the number of redirects , At most one redirection is allowed
curl_easy_setopt(pcurl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_getinfo(pcurl, CURLINFO_REDIRECT_URL, &redirect_url);
//if (redirect_url != nullptr)
curl_free(redirect_url);
}
}
Complete test demo:
struct CurlDownloadContext
{
std::string redirect_url;
CurlDownloadContext()
{
}
~CurlDownloadContext()
{
}
};
size_t HeaderCallback4Download(char *ptr,size_t size,size_t nmemb,void *userdata)
{
// adopt Location: Get redirection url
CurlDownloadContext* dld_ctx = (CurlDownloadContext*)userdata;
const char key_loc[] = "Location:";
if (_strnicmp(ptr, key_loc, strlen(key_loc)) == 0)
{
dld_ctx->redirect_url.assign(ptr + strlen(key_loc));
// Get rid of \r\n
if (dld_ctx->redirect_url.size() > 2)
{
dld_ctx->redirect_url.resize(dld_ctx->redirect_url.size() - 2);
}
}
return size * nmemb;
}
int _tmain(int argc, _TCHAR* argv[])
{
curl_global_init(CURL_GLOBAL_ALL);
CURL *pcurl = curl_easy_init();
CurlDownloadContext dld_ctx;
::curl_easy_setopt(pcurl, CURLOPT_URL, "http://weibo.com/");
::curl_easy_setopt(pcurl, CURLOPT_HTTPGET, 1L);
::curl_easy_setopt(pcurl, CURLOPT_CONNECTTIMEOUT, 30000L);
::curl_easy_setopt(pcurl, CURLOPT_LOW_SPEED_TIME, 1800L);
::curl_easy_setopt(pcurl, CURLOPT_LOW_SPEED_LIMIT, 10L);
::curl_easy_setopt(pcurl, CURLOPT_SSL_VERIFYPEER, 0L);
::curl_easy_setopt(pcurl, CURLOPT_SSL_VERIFYHOST, 0L);
::curl_easy_setopt(pcurl, CURLOPT_NOSIGNAL, 1L);
::curl_easy_setopt(pcurl, CURLOPT_HEADERFUNCTION, HeaderCallback4Download);
::curl_easy_setopt(pcurl, CURLOPT_HEADERDATA, &dld_ctx);
//::curl_easy_setopt(pcurl, CURLOPT_WRITEFUNCTION, WriteCallback4Download);
//::curl_easy_setopt(pcurl, CURLOPT_WRITEDATA, &dld_ctx);
CURLcode ret = ::curl_easy_perform(pcurl);
long httpcode;
curl_easy_getinfo(pcurl, CURLINFO_RESPONSE_CODE, &httpcode);
if (httpcode == 301 || httpcode == 302)
{
char *redirect_url = NULL;
//1 Indicates the number of redirects , At most one redirection is allowed
curl_easy_setopt(pcurl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_getinfo(pcurl, CURLINFO_REDIRECT_URL, &redirect_url);
//if (redirect_url != nullptr)
curl_free(redirect_url);
}
curl_global_cleanup();
return 0;
}
边栏推荐
- C语言数组,函数,操作符,关键字入门
- leetcode-343. 整数拆分
- 大一暑假实习day5_1
- Code random notes_ Linked list_ 206 reverse linked list
- VRTK功能教学(二):Unity3DVRTK手柄瞬移和UI交互射线切换功能丨3D模型射线交互切换丨直线和曲线的切换
- With only 5000 lines of code, AI rendered 100 million landscape paintings on Quanzhi v853
- 浏览器插件损坏怎么办,删除|安装插件的方法
- Detailed explanation of Axi agreement
- Code random notes_ Linked list_ 24 exchange nodes in the linked list in pairs
- C语言中的冒泡排序
猜你喜欢
随机推荐
"Dial" out the number on the number of digits - a variety of ideas to achieve reverse output of a four digit number
leetcode-494.目标和
C语言中带指针和不带指针交换变量值
Vs2019 project packaging exe tutorial
《ctfshow_web入门 信息搜集|CSDN创作打卡》;
Swap parity bits in binary bits
最大公约数和最小公倍数
Druid source code reading 8-druiddatasource removeabandoned mechanism
Watermelon book machine learning notes -- naive Bayes
12月1日作业
Hefei University of technology information theory and coding course design, including code, visual interface, course design report
Day7 summary of freshman Summer Internship
Filter拦截器和过滤器
Leetcode-494. objectives and
Code random notes_ Array_ 27 remove elements
Leetcode-583. Deleting two strings
C语言实现三子棋
编程小白的第一篇博客
Detailed explanation of Axi agreement
Today's homework blog







