如何解决HTTPS状态码404?

时间:2015-04-02 15:20:59

标签: winhttp

我收到以下代码段的状态代码404。

注意:当您试图找出问题时,请添加<HOSTNAME>

HINTERNET                         m_hConnect;                    
HINTERNET                         m_hSession;                      
HINTERNET                         m_hRequest;                    

m_hSession = WinHttpOpen(userAgent,
                WINHTTP_ACCESS_TYPE_NO_PROXY,
                WINHTTP_NO_PROXY_NAME,
                WINHTTP_NO_PROXY_BYPASS, 0);

if (!m_hSession) {
                printf("WinHttpOpen:Error %d has occurred.",GetLastError());
                return 0;
}
// connect to the IPP server on the given port
m_hConnect = WinHttpConnect( m_hSession, <HOSTNAME>, 443, 0);

if (!m_hConnect) {
                printf("WinHttpConnect: Error %d has occurred.",GetLastError());
                return 0;
}

// Create an HTTP Request handle.
m_hRequest = WinHttpOpenRequest(m_hConnect, L"POST" ,L"/ipp",
                L"HTTP/1.1", WINHTTP_NO_REFERER,
                WINHTTP_DEFAULT_ACCEPT_TYPES,WINHTTP_FLAG_SECURE);

if (!m_hRequest) {
                printf("WinHttpOpenRequest:Error %d has occurred.",GetLastError());
                return 0;
}

DWORD secureflags = SECURITY_FLAG_IGNORE_UNKNOWN_CA | SECURITY_FLAG_IGNORE_CERT_CN_INVALID |SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE | SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;

BOOL bResult= WinHttpSetOption(m_hRequest, WINHTTP_OPTION_SECURITY_FLAGS, (LPVOID)&secureflags, sizeof(secureflags));
if (!bResult) {
                printf("WinHttpSetOption:Error %d has occurred.",GetLastError());
                return 0;
}

bResult = WinHttpSendRequest(m_hRequest, WINHTTP_NO_ADDITIONAL_HEADERS,0,WINHTTP_NO_REQUEST_DATA,0,0,0);
if (!bResult) {
                printf("WinHttpSendRequest:Error %d has occurred.",GetLastError());
                return 0;
}

if( !WinHttpReceiveResponse( m_hRequest, NULL ) )
{
                if( GetLastError( ) == ERROR_WINHTTP_CLIENT_AUTH_CERT_NEEDED )
                {
                                HCERTSTORE hMyStore = 0;
                                PCCERT_CONTEXT pCertContext = 0;
                                WCHAR szCertName[256]={0};
                                //MY is the store the certificate is in.
                                hMyStore = CertOpenSystemStore( 0, TEXT("MY") );
                                if( hMyStore )
                                {
                                                pCertContext = CertFindCertificateInStore( hMyStore,
                                                                X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
                                                                0,
                                                                CERT_FIND_SUBJECT_STR,
                                                                (LPVOID) szCertName, //Subject string in the certificate.
                                                                NULL );
                                                if( pCertContext )
                                                {
                                                                WinHttpSetOption( m_hRequest, 
                                                                                WINHTTP_OPTION_CLIENT_CERT_CONTEXT,
                                                                                (LPVOID) pCertContext, 
                                                                                sizeof(CERT_CONTEXT) );
                                                                CertFreeCertificateContext( pCertContext );
                                                }
                                                CertCloseStore( hMyStore, 0 );

                                                // NOTE: Application should now resend the request.
                                                bResult = WinHttpSendRequest(m_hRequest, WINHTTP_NO_ADDITIONAL_HEADERS,0,WINHTTP_NO_REQUEST_DATA,0,0,0);
                                                if (!bResult) {
                                                                printf("WinHttpSendRequest:Error %d has occurred.",GetLastError());
                                                                return 0;
                                                }
                                }
                }
}


DWORD dwStatusCode = 0;
DWORD dwSize = sizeof(DWORD);
BOOL  bResults = true;

bResults = WinHttpQueryHeaders( m_hRequest, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, NULL, &dwStatusCode, &dwSize, NULL );

结果:我的状态代码为404.我无法弄清楚它为什么会发生。

使用WinINet,我遵循了相同的程序,我获得了200的状态代码,这是成功的。但我的要求是只使用WinHttp

1 个答案:

答案 0 :(得分:0)

这不是一个谜。这是因为找不到您请求的资源。

或者:

  1. 您使用的网址不正确,或
  2. 将URL映射到服务器上的资源是不正确的。