http.h
#ifndef _HTTP_H_
#define _HTTP_H_
#include
#include
#include
namespace tools
{
class CCurlWrapper
{
public:
CCurlWrapper ();
~CCurlWrapper ();
int PostFile (const std::string& strUrl, const std::string& strRequest, std::string& strReply);
int GetFile (const std::string& strUrl, std::string& strReply);
void AddHttpHeader (const std::string& strHeader);
private:
int Init ();
static size_t WriteDataCallback (void *ptr, size_t size, size_t nCount, void *pData);
CURL *m_pCurl;
std::set m_setHeaders;
};
}
#endif
http.cpp
#include "http.h"
#include
using namespace std;
using namespace tools;
CCurlWrapper::CCurlWrapper () :
m_pCurl(NULL)
{
//curl_global_init(CURL_GLOBAL_ALL);
Init();
}
CCurlWrapper::~CCurlWrapper ()
{
curl_easy_cleanup(m_pCurl);
m_pCurl = NULL;
}
int CCurlWrapper::Init ()
{
if (m_pCurl)
{
curl_easy_cleanup(m_pCurl);
m_pCurl = NULL;
}
m_pCurl = curl_easy_init();
if (m_pCurl == NULL)
{
return -1;
}
// set connection timeout to 10's
curl_easy_setopt(m_pCurl, CURLOPT_CONNECTTIMEOUT, 10);
// set timeout to 30's
curl_easy_setopt(m_pCurl, CURLOPT_TIMEOUT, 30);
// accept identiy, deflate, and gzip encoding. (Accept-Encoding: )
curl_easy_setopt(m_pCurl, CURLOPT_ENCODING, "gzip, deflate");
// set user-agent to that of MSIE6
curl_easy_setopt(m_pCurl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
// let curl to follow location (auto handle HTTP 301, 302)
curl_easy_setopt(m_pCurl, CURLOPT_FOLLOWLOCATION, 1);
m_setHeaders.clear();
m_setHeaders.insert("Accept-Language: zh-cn");
// output debug info (for debug only)
//curl_easy_setopt(m_pCurl, CURLOPT_VERBOSE, true);
curl_easy_setopt(m_pCurl, CURLOPT_VERBOSE, false);
return 0;
}
void CCurlWrapper::AddHttpHeader (const string& strHeader)
{
m_setHeaders.insert(strHeader);
}
int CCurlWrapper::PostFile (const string& strUrl, const string& strRequest, string& strReply)
{
if (m_pCurl == NULL)
return -1;
int iRet;
if (strUrl.empty())
{
return -2;
}
curl_easy_setopt(m_pCurl, CURLOPT_URL, strUrl.c_str());
stringstream ssReply;
curl_easy_setopt(m_pCurl, CURLOPT_WRITEDATA, &ssReply);
curl_easy_setopt(m_pCurl, CURLOPT_WRITEFUNCTION, WriteDataCallback);
// set post data
if (strRequest.empty())
{
return -3;
}
curl_easy_setopt(m_pCurl, CURLOPT_POST, true);
curl_easy_setopt(m_pCurl, CURLOPT_POSTFIELDS, strRequest.c_str());
curl_easy_setopt(m_pCurl, CURLOPT_POSTFIELDSIZE, strRequest.length());
// autosave cookie with the handle
curl_easy_setopt(m_pCurl, CURLOPT_COOKIEFILE, "/dev/null");
// set header
struct curl_slist *headers = NULL;
if (!m_setHeaders.empty())
{
for (set::iterator it = m_setHeaders.begin(); it != m_setHeaders.end(); ++it)
headers = curl_slist_append(headers, it->c_str());
}
curl_easy_setopt(m_pCurl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(m_pCurl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(m_pCurl, CURLOPT_SSL_VERIFYHOST, 0L);
//curl_easy_setopt(m_pCurl, CURLOPT_WRITEDATA, &ssReply);
//curl_easy_setopt(m_pCurl, CURLOPT_WRITEFUNCTION, WriteDataCallback);
// perform
iRet = curl_easy_perform(m_pCurl);
if (iRet == CURLE_OK)
{
strReply = ssReply.str();
}
if (headers)
curl_slist_free_all(headers);
return (iRet == CURLE_OK) ? 0 : iRet;
}
int CCurlWrapper::GetFile (const string& strUrl, string& strReply)
{
if (m_pCurl == NULL)
return -1;
int iRet;
if (strUrl.empty())
{
return -2;
}
curl_easy_setopt(m_pCurl, CURLOPT_URL, strUrl.c_str());
stringstream ssReply;
curl_easy_setopt(m_pCurl, CURLOPT_WRITEDATA, &ssReply);
curl_easy_setopt(m_pCurl, CURLOPT_WRITEFUNCTION, WriteDataCallback);
curl_easy_setopt(m_pCurl, CURLOPT_HTTPGET, true);
// autosave cookie with the handle
curl_easy_setopt(m_pCurl, CURLOPT_COOKIEFILE, "/dev/null");
// set header
struct curl_slist *headers = NULL;
if (!m_setHeaders.empty())
{
for (set::iterator it = m_setHeaders.begin(); it != m_setHeaders.end(); ++it)
headers = curl_slist_append(headers, it->c_str());
}
curl_easy_setopt(m_pCurl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(m_pCurl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(m_pCurl, CURLOPT_SSL_VERIFYHOST, 0L);
//curl_easy_setopt(m_pCurl, CURLOPT_WRITEDATA, &ssReply);
//curl_easy_setopt(m_pCurl, CURLOPT_WRITEFUNCTION, WriteDataCallback);
// perform
iRet = curl_easy_perform(m_pCurl);
if (iRet == CURLE_OK)
{
strReply = ssReply.str();
}
if (headers)
curl_slist_free_all(headers);
return (iRet == CURLE_OK) ? 0 : iRet;
}
size_t CCurlWrapper::WriteDataCallback (void *ptr, size_t size, size_t nCount, void *pData)
{
stringstream* pTmp = (stringstream*) pData;
pTmp->write((char*) ptr, size * nCount);
return size * nCount;
}
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
相关推荐: CCF 习题 201312-5 I’m stuck! (BFS)
题意不说了,中文的题意。 思路: 普通的bfs即可! 第一个bfs,判断由起点是否可以到达终点,并且从起点能到达的点标记出来!(方便第二个bfs),如果可以到达终点,ok =1 ,最后只需要判断ok 是不是1 就可以处理其中一个答案! 第二个bfs 判断 …