kaixuan1115 / notes

笔记收录
6 stars 0 forks source link

MFC访问HTTPS服务忽略证书错误 #30

Closed xiaokaixuan closed 4 years ago

xiaokaixuan commented 4 years ago

用VC++编程访问HTTPS服务时,遇到过“认证作者无效或者不正确”,“证书已失效”及类似的问题相当棘手,在这里我把可以忽略这些认证的方法记下来,让遇到相同问题的朋友可以更快解决问题。 下面贴上代码:(可以直接使用,需要包含 afxinet.h )


VOID HttpsPost(LPCTSTR pszUrl)
{
DWORD dwServerType(0);
CString strServer, strObject;
INTERNET_PORT nPort(80);
CInternetSession sess; // 同一个Session自动记录Cookie
try { do
{
if (!AfxParseURL(pszUrl, dwServerType, strServer, strObject, nPort)) break;
CHttpConnection* pConn = sess.GetHttpConnection(strServer, INTERNET_FLAG_SECURE, nPort); // HTTPS安全设置
if (!pConn) break;
CHttpFile* pHttpFile = pConn->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject, NULL, 1, NULL, NULL,
INTERNET_FLAG_SECURE | INTERNET_FLAG_EXISTING_CONNECT | 
INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE | 
INTERNET_FLAG_IGNORE_CERT_DATE_INVALID | INTERNET_FLAG_IGNORE_CERT_CN_INVALID); // HTTPS忽略证书
if (!pHttpFile) break;
    DWORD dwFlags(0);
    pHttpFile->QueryOption(INTERNET_OPTION_SECURITY_FLAGS, dwFlags);
    dwFlags |= SECURITY_FLAG_IGNORE_REVOCATION | SECURITY_FLAG_IGNORE_UNKNOWN_CA;
    pHttpFile->SetOption(INTERNET_OPTION_SECURITY_FLAGS, dwFlags); // HTTPS忽略证书

    pHttpFile->AddRequestHeaders(_T("Content-Type: application/x-www-form-urlencoded"));
    CHAR szParam[1024] = "username=xiaokaixuan&password=password";
    pHttpFile->SendRequest(NULL, 0, LPVOID(szParam), lstrlenA(szParam));

    DWORD dwRetCode(0);
    pHttpFile->QueryInfo(HTTP_QUERY_STATUS_CODE, dwRetCode);
    if (dwRetCode < 200 || dwRetCode >= 300) break;
    CHAR Buffer[2048] = { 0 };
    pHttpFile->Read(Buffer, 2047);
    pHttpFile->Close();
    delete pHttpFile;
    delete pConn;
    AfxMessageBox(CString(Buffer));
} while (0); } catch (CInternetException *pExcept) {
    CString strError;
    pExcept->GetErrorMessage(strError.GetBuffer(2048), 2048);
    strError.ReleaseBuffer();
    pExcept->Delete();
    AfxMessageBox(strError);
}

}

xiaokaixuan commented 4 years ago

阿里免费图床


@echo off

title Alicdn Image

echo. set filename=%~1 if not "%filename: =%" == "%filename%" ( echo ERROR: Filename can not contain space !!! & goto :eof )

if not exist %filename% ( echo ERROR: Filename is not exists !!! & goto :eof )

echo Uploading ... echo.

curl "https://kfupload.alibaba.com/mupload" ^ -F "scene=productImageRule" ^ -F "name=image.png" ^ -F "file=@%filename%;type=image/png"