void Socket::http_get(const char *uri, std::vector<std::string>& header, std::vector<std::string>& resheader, std::vector<unsigned char>& resdata) { resheader.clear(); resdata.clear(); if (strncmp(uri, "http://", 7) != 0) { return; } http_request_t http = parse_http(uri); socket(AF_INET, SOCK_STREAM, 0); connect(http.name.c_str(), http.port); std::string req; req = std::string("GET ") + http.query + " HTTP/1.1\r\n"; req += std::string("User-Agent: ") + USER_AGENT + "\r\n"; req += std::string("Host: ") + http.name + "\r\n"; for (std::vector<std::string>::iterator it = header.begin(); it != header.end(); it++ ) { req += *it; req += "\r\n"; } req += "\r\n"; write(req.c_str(), req.size()); parse_http_result(resheader, resdata); }
void Socket::http_post(char const *uri, char const *content, size_t contentlen, std::vector<std::string> const *header, std::vector<std::string> *resheader, std::vector<unsigned char> *resdata) { resheader->clear(); resdata->clear(); if (strncmp(uri, "http://", 7) != 0) { return; } http_request_t http = parse_http(uri); socket(AF_INET, SOCK_STREAM, 0); connect(http.name.c_str(), http.port); std::string req; req = "POST "; req += http.query; req += " HTTP/1.1\r\n"; std::vector<std::string> preheader; preheader.push_back(std::string("User-Agent: ") + USER_AGENT); preheader.push_back("Host: " + http.name); preheader.push_back("Accept: */*"); preheader.push_back("Content-Length: " + to_s(contentlen)); preheader.push_back("Content-Type: application/x-www-form-urlencoded"); for (std::vector<std::string>::const_iterator it = preheader.begin(); it != preheader.end(); it++) { req += *it; req += "\r\n"; } if (header) { for (std::vector<std::string>::const_iterator it = header->begin(); it != header->end(); it++) { req += *it; req += "\r\n"; } } req += "\r\n"; write(req.c_str(), req.size()); write(content, contentlen); // std::vector<unsigned char> data; read(&data); if (!data.empty()) { unsigned char const *begin = &data[0]; unsigned char const *end = begin + data.size(); parse_http_result(begin, end, resheader, resdata); } }
void Socket::http_post(const char *uri, std::vector<std::string>& header, const void *content, size_t contentlen, const char *contet_type, std::vector<std::string>& resheader, std::vector<unsigned char>& resdata) { resheader.clear(); resdata.clear(); if (strncmp(uri, "http://", 7) != 0) { return; } http_request_t http = parse_http(uri); socket(AF_INET, SOCK_STREAM, 0); connect(http.name.c_str(), http.port); std::string req; req = std::string("POST ") + http.query + " HTTP/1.1\r\n"; req += std::string("User-Agent: ") + USER_AGENT + "\r\n"; req += std::string("Host: ") + http.name + "\r\n"; req += std::string("Content-Length: ") + to_s(contentlen) + "\r\n"; if (contet_type) { req += std::string("Content-Type: ") + contet_type + "\r\n"; } else { req += std::string("Content-Type: application/x-www-form-urlencoded\r\n"); } for (std::vector<std::string>::iterator it = header.begin(); it != header.end(); it++ ) { req += *it; req += "\r\n"; } req += "\r\n"; //OutputDebugStringA(req.c_str()); write(req.c_str(), req.size()); write(content, contentlen); parse_http_result(resheader, resdata); //OutputDebugStringA((const char*)&resdata[0]); }