Created
July 25, 2013 10:50
-
-
Save hangingman/6078656 to your computer and use it in GitHub Desktop.
curlpp sample code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <sstream> | |
#include <iostream> | |
#include <fstream> | |
#include <curlpp/cURLpp.hpp> | |
#include <curlpp/Easy.hpp> | |
#include <curlpp/Options.hpp> | |
#include <curlpp/Infos.hpp> | |
namespace | |
{ | |
const long MyPort = 80; | |
} | |
/** | |
* test code for MacOSX 10.7 | |
*/ | |
int main(int, char **) | |
{ | |
try | |
{ | |
const std::string userAgent = "Monazilla/1.00 Unknown"; | |
const std::string outputPath = "./list.gzip"; | |
const std::string url = "http://menu.2ch.net/bbsmenu.html"; | |
std::list<std::string> headers; | |
headers.push_back("Accept-Encoding: gzip"); | |
headers.push_back("Host: menu.2ch.net"); | |
headers.push_back("Accept: "); | |
headers.push_back("Referer: http://menu.2ch.net/"); | |
headers.push_back("Accept-Language: ja"); | |
headers.push_back("User-Agent: " + userAgent); | |
curlpp::Cleanup myCleanup; | |
curlpp::Easy myRequest; | |
myRequest.setOpt(new curlpp::options::Url(url)); | |
myRequest.setOpt(new curlpp::options::HttpHeader(headers)); | |
myRequest.setOpt(new curlpp::options::Timeout(5)); | |
myRequest.setOpt(new curlpp::options::Verbose(true)); | |
// メインのデータ出力 | |
std::ofstream ofs(outputPath, std::ios::out | std::ios::trunc | std::ios::binary ); | |
curlpp::options::WriteStream ws(&ofs); | |
myRequest.setOpt(ws); | |
myRequest.perform(); | |
} | |
catch( curlpp::RuntimeError &e ) | |
{ | |
std::cout << e.what() << std::endl; | |
} | |
catch( curlpp::LogicError &e ) | |
{ | |
std::cout << e.what() << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment