MFC发送http https以及json解析

1 篇文章 0 订阅

域名解析成IP

		char szWeb[128] = "www.baidu.com";
		struct hostent *pHost = NULL;
		pHost = gethostbyname(szWeb);//完成主机名到域名的解析
		char *IP = inet_ntoa(*((struct in_addr *)pHost->h_addr));
		CString ipStr = IP;

请求三部曲:

1、CInternetSession session;

		CInternetSession session;
		session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 1000 * 20);

2、CHttpConnection* pConnection;

		CHttpConnection* pConnection;
		pConnection = session.GetHttpConnection(strServer, wPort);

3、pConnection->OpenRequest

		//https
		pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, ("post.do"), NULL, 1, NULL, NULL, INTERNET_FLAG_SECURE
				| INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_RELOAD | INTERNET_FLAG_IGNORE_CERT_CN_INVALID | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID);
		//http
		pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, ("post.do"));

4、pFile->SendRequest(szHeaders, (LPVOID)reqchar, strlen(reqchar));

		pFile->SendRequest(szHeaders, (LPVOID)reqchar, strlen(reqchar));

5、pFile->QueryInfoStatusCode(dwRet)

https请求

			CString strURL = "https://209.144.91.204:443/";
			CString strServer, strObject;
			INTERNET_PORT wPort;
			DWORD dwType;
			if (!AfxParseURL(strURL, dwType, strServer, strObject, wPort))
			{
				return false;//URL解析错误
			}
			pConnection = session.GetHttpConnection(strServer, wPort);						//二、连接到Http服务器:
			if (NULL == pConnection)
			{
				return false;
			}
			pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, ("/post.do"), NULL, 1, NULL, NULL, INTERNET_FLAG_SECURE
				| INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_RELOAD | INTERNET_FLAG_IGNORE_CERT_CN_INVALID | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID);

http请求

			CString dnsUrl = ipStr + ":80";
			pConnection = session.GetHttpConnection(url);
			pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, ("/post.do"));

json解析

			int len = pFile->GetLength();
			char buf[2048];
			int numread;
			Json::Reader jsonReader;
			Json::Value root;
			while ((numread = pFile->Read(buf, sizeof(buf) - 1)) > 0)
			{
				buf[numread] = '\0';
				strFile += buf;
			}
			if (log)
				AfxMessageBox(strFile);
			if (jsonReader.parse(buf, root)) {
				int retCode = root["retcode"].asInt();
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值