VC++ 如何获取web api返回的json或者xml数据
VC++ 怎么获取web api返回的json或者xml数据
例如 获取http://www.xxxx.com/users 返回的json 或者xml数据
VC++ 怎么实现呢
------解决方案--------------------
发送Http请求,获取返回的数据,xml或者Json需要解析才能得到你想要的内容,xml解析相对Json好解析一点。
以下代码仅供参考,可以百度或google搜索一下,有很多。
例如 获取http://www.xxxx.com/users 返回的json 或者xml数据
VC++ 怎么实现呢
------解决方案--------------------
发送Http请求,获取返回的数据,xml或者Json需要解析才能得到你想要的内容,xml解析相对Json好解析一点。
以下代码仅供参考,可以百度或google搜索一下,有很多。
#include <stdio.h>
#include <windows.h>
#include <wininet.h>
#define MAXSIZE 1024
#pragma comment(lib, "Wininet.lib")
void urlopen(_TCHAR*);
int _tmain(int argc, _TCHAR* argv[])
{
urlopen(_T("http://coderzh.cnblogs.com"));
return 0;
}
void urlopen(_TCHAR* url)
{
HINTERNET hSession = InternetOpen(_T("UrlTest"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if(hSession != NULL)
{
HINTERNET hHttp = InternetOpenUrl(hSession, url, NULL, 0, INTERNET_FLAG_DONT_CACHE, 0);
if (hHttp != NULL)
{
wprintf_s(_T("%s\n"), url);
BYTE Temp[MAXSIZE];
ULONG Number = 1;
while (Number > 0)
{
InternetReadFile(hHttp, Temp, MAXSIZE - 1, &Number);
Temp[Number] = '\0';
printf("%s", Temp);
}
InternetCloseHandle(hHttp);
hHttp = NULL;
}
InternetCloseHandle(hSession);
hSession = NULL;
}
}