In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Scene description
Get weather information from the t.weather.sojson.com web page. If you do not use the libcurl library, you need to implement Transfer-Encoding: chunked block receiving and Content-Encoding: gzip decompression. Now provide the libcurl implementation code.
Code
Size_t WriteResponseBody (void * ptr, size_t size, size_t nmemb, void * userData)
{
Std::string* pStrBuffer = (std::string*) userData
Size_t nLen = size * nmemb
PStrBuffer- > append ((char*) ptr, nLen)
Return nLen
}
Int GetWeatherUsingCurl (const std::string& strUrl, std::string& strResponseData)
{
CURL * pCurlHandle = curl_easy_init ()
Curl_easy_setopt (pCurlHandle, CURLOPT_CUSTOMREQUEST, "GET")
Curl_easy_setopt (pCurlHandle, CURLOPT_URL, strUrl.c_str ())
Curl_easy_setopt (pCurlHandle, CURLOPT_WRITEFUNCTION, WriteResponseBody); / / set callback function
/ / curl_easy_setopt (pCurlHandle, CURLOPT_HEADER, 1); / / Save HTTP header information to strResponseData
Curl_easy_setopt (pCurlHandle, CURLOPT_WRITEDATA, & strResponseData); / / set the parameters of the callback function to get feedback information
Curl_easy_setopt (pCurlHandle, CURLOPT_TIMEOUT, 15); / / timeout setting when receiving data. If the data is not received within 10 seconds, exit directly.
Curl_easy_setopt (pCurlHandle, CURLOPT_MAXREDIRS, 1); / / search times to prevent search too deep
Curl_easy_setopt (pCurlHandle, CURLOPT_CONNECTTIMEOUT, 5); / / connection timeout. If this value is set too short, it may disconnect if the data is not requested.
CURLcode nRet = curl_easy_perform (pCurlHandle)
Curl_easy_cleanup (pCurlHandle)
Return nRet
}
Call example
Std::string strResponseData
GetWeatherUsingCurl ("t.weather.sojson.com/api/weather/city/101200101", strResponseData)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.