当前位置:网站首页>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;
}

Redirect online detection

原网站

版权声明
本文为[CAir2]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207221753517789.html