直接上代码:
#include <afxinet.h>
void CTestJsonDlg::OnBnClickedButton1()
{
//使用百度api获取数据-----------------------------------------------------------------
char buf[1000] = { 0 };
CString strIDCard;
CInternetSession session;
CHttpFile* file = NULL; int nRead;
file = (CHttpFile *)session.OpenURL(_T("http://apistore.baidu.com/microservice/icardinfo?id=xxxxxxxxxxxxxxxxxx"));//id为身份证号码,需要自己输入
if (NULL != file){
//Do something here with the web request
//Clean up the file here to avoid a memory leak!!!!!!!
while ((nRead = file->Read(buf, sizeof(buf))) > 0){
strIDCard = buf;
}
GetDlgItem(IDC_ID_CARD)->SetWindowText(strIDCard );
file->Close();
delete file;
}
session.Close();
}
解析获取的json字符串见下篇文章。