libcurl部分使用方法

POST

void NetworkOperator::PostData(string url, string data)
{
	m_curl = curl_easy_init();
	if (m_curl == NULL)
	{
		throw runtime_error("init the curl false,please check");
	}
	curl_slist* pList = NULL;
	pList = curl_slist_append(pList, "Content-Type:application/x-www-form-urlencoded; charset=UTF-8");

	curl_easy_setopt(m_curl, CURLOPT_HTTPHEADER, pList);

	curl_easy_setopt(m_curl, CURLOPT_TIMEOUT, 5);		//5s timeout
	curl_easy_setopt(m_curl, CURLOPT_NOSIGNAL, 1L);
	curl_easy_setopt(m_curl, CURLOPT_HEADER, 1);
	curl_easy_setopt(m_curl, CURLOPT_POST, 1);

	string handleData = "ckdata=";
	handleData.append(data);
	curl_easy_setopt(m_curl, CURLOPT_POSTFIELDS, handleData.c_str());
	curl_easy_setopt(m_curl, CURLOPT_POSTFIELDSIZE, handleData.size());

	cout << "handleData: " << endl << handleData << endl;

	string response;
	curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &response);
	curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, writePostData);
	curl_easy_setopt(m_curl, CURLOPT_URL, url.c_str());

	try
	{
		m_res = curl_easy_perform(m_curl);
	}
	catch (const std::exception& e)
	{
		cout << e.what() << endl;
	}

	//long totaltime = 0;	//获取返回码必须为long,否则在释放时会段错误
	//curl_easy_getinfo(m_curl, CURLINFO_RESPONSE_CODE, &totaltime);
	//cout << "responseCode: " << totaltime << endl;
	cout << "responseData: " << endl << response << endl;

}

HOST

void NetworkOperator::GetTheRequestVelocityAndStatus(string url, std::stringstream& streamData)
{
	string responseData;

	string urlData = this->ReadFromUrl(url);
	cout << urlData << endl;
	vector urlVector;
	split(urlVector, urlData, is_any_of("\n"));

	curl_easy_setopt(m_curl, CURLOPT_TIMEOUT, 5);		//5s timeout
	curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &responseData);
	curl_easy_setopt(m_curl, CURLOPT_FOLLOWLOCATION, true); //enable the relocation
	curl_easy_setopt(m_curl, CURLOPT_MAXREDIRS, 15);	//the max reloc ation times
	curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, writer);
	//curl -v
	//curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1);
	curl_easy_setopt(m_curl, CURLOPT_SSL_VERIFYPEER, false);

	for (auto i : urlVector)
	{
		vector dataVec;
		split(dataVec, i, is_any_of("|"));
		curl_slist* pList = NULL;
		string header;
		header.append("Host:");
		header.append(dataVec[2]);
		pList = curl_slist_append(pList, header.c_str());
		//pList = curl_slist_append(pList, "Accept: Agent-007");
		//curl_easy_setopt(m_curl, CURLOPT_HEADER, 1);
		curl_easy_setopt(m_curl, CURLOPT_HTTPHEADER, pList);
		string addr = dataVec[0];
		curl_easy_setopt(m_curl, CURLOPT_URL, addr.c_str());

		try
		{
			m_res = curl_easy_perform(m_curl);
		}
		catch (const std::exception& e)
		{
			cout << e.what() << endl;
		}
		curl_slist_free_all(pList);

		double totaltime = 0;
		curl_easy_getinfo(m_curl, CURLINFO_TOTAL_TIME, &totaltime);
		streamData << dataVec.at(1) << "|" << dataVec.at(2) << "|" << totaltime << "|";

		if (responseData == dataVec.at(3))
			streamData << 1;
		else
			streamData << 0;

		streamData << endl;
		responseData.clear();
	}
}

 

你可能感兴趣的:(C++,curl)