【实用可测】C++ Https访问不安全证书服务器代码

void CHttpsDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	CString strUrl = "https://auth.11111.com.cn/verify?";

	DWORD dwFlags;
	DWORD dwStatus = 0;
	DWORD dwStatusLen = sizeof(dwStatus);
	CString strLine;
		
	DWORD dwServerType;
	CString strServer, strObject;
	INTERNET_PORT nPort;
	AfxParseURL(strUrl, dwServerType, strServer, strObject, nPort);
		
	CString strHtml;
	CInternetSession session;
	try 
	{
		CHttpConnection* pHttpConnect = session.GetHttpConnection(strServer, INTERNET_FLAG_SECURE, nPort, NULL, NULL);
		if(pHttpConnect) 
		{
			CHttpFile* pHttpFile = (CHttpFile*)pHttpConnect->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject, NULL, 1,
					NULL, NULL,
					INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_KEEP_CONNECTION|
					INTERNET_FLAG_SECURE  | INTERNET_FLAG_IGNORE_CERT_CN_INVALID | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID
					//SECURITY_FLAG_IGNORE_REVOCATION
					);
			//get web server option
			pHttpFile->QueryOption(INTERNET_OPTION_SECURITY_FLAGS, dwFlags);
			dwFlags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;
			dwFlags |= SECURITY_FLAG_IGNORE_REVOCATION;<span style="white-space:pre">	</span>//这两个是核心参数
			//set web server option
			pHttpFile->SetOption(INTERNET_OPTION_SECURITY_FLAGS, dwFlags);
			if(pHttpFile->SendRequest()) 
			{
				//get response status if success, return 200
				pHttpFile->QueryInfo(HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_STATUS_CODE, &dwStatus, &dwStatusLen, 0);
				while(pHttpFile->ReadString(strLine)) 
				{                   
					strHtml += strLine + char(13) + char(10);
				}
				CFile file;
				if (file.Open("1.txt", CFile::modeCreate | CFile::modeWrite))
				{
					file.Write(strHtml.GetBuffer(0), strHtml.GetLength());
				}
				file.Flush();
				file.Close();
				} 
			else 
			{
				AfxMessageBox(_T("SendRequest fail!"));
			}
		} 
		else 
		{
			AfxMessageBox(_T("Connect fail!"));
		}
		} 
		catch(CInternetException *e) 
		{
		//	e->ReportError();
			CString strError;
			strError.Format("%d", e->m_dwError);
			AfxMessageBox(strError);
		}
		
		if(dwStatus >= 200 && dwStatus < 300)
		{
			AfxMessageBox(_T("True!"));
		//return true;
		} 
		else 
		{
			AfxMessageBox(_T("False!"));
		//return false;
		}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值