谢谢你们的所有评论。我搜索并使用 cURL 库来解决我的问题。可能是一些示例最简单的方法。还要感谢上一个主题,它有很大帮助:How do I download xml from the internet in C++
size_t AppendDataToStringCurlCallback(void *ptr, size_t size, size_t nmemb, void *vstring)
{
std::string * pstring = (std::string*)vstring;
pstring->append((char*)ptr, size * nmemb);
return size * nmemb;
}
std::string DownloadUrlAsString(const std::string & url)
{
std::string body;
CURL *curl_handle;
curl_global_init(CURL_GLOBAL_ALL);
curl_handle = curl_easy_init();
curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, AppendDataToStringCurlCallback);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &body);
curl_easy_perform(curl_handle);
curl_easy_cleanup(curl_handle);
return body;
}
int main(void)
{
CURL *curl;
CURLcode res;
std::string data_to_read;
curl = curl_easy_init();
if(curl) {
//curl_easy_setopt(curl, CURLOPT_URL, "www.earthtools.org/timezone/0/0");
data_to_read=DownloadUrlAsString("www.earthtools.org/timezone/0/0");
res = curl_easy_perform(curl);
// always cleanup
curl_easy_cleanup(curl);
}
std::string str=data_to_read;
std::string str2 ("<utctime>");
std::size_t found = str.find(str2);
if (found!=std::string::npos)
std::cout << "first data found at: " << found << '\n';
//data_to_read[found+9]
char year[4];//="yyyy";
char month[2];//="mm";
char day[2];//="dd";
int delta=0;
int start=found+9;
for (int k=start; k<start+10;k++)
{
if(k<start+4){
year[delta]= data_to_read[k];
delta++;
}
else if (k==start+4)
delta=0;
else if ((k>=start+5) && (k<=start+6))
{
month[delta]=data_to_read[k];
delta++;
}
else if (k==start+7)
delta=0;
else if ((k>=start+8) && (k<=start+9))
{
day[delta]=data_to_read[k];
delta++;
}
}
int year_m=atoi(year);
int month_m=atoi(month);
int day_m=atoi(day);