天天看点

【实用可测】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;
		}
}
           

继续阅读