1

I am getting status code 404 for the below code snippet.

Note: please add <HOSTNAME> when you are trying to figure out the issue.

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 );

Result: I am getting status code of 404. I am not able to figure out why it is happening.

Using WinINet, I have followed the same procedure, I am getting status code of 200, which is success. But my requirement is to use only WinHttp.

halfer
  • 19,824
  • 17
  • 99
  • 186
Siva
  • 1,281
  • 2
  • 19
  • 41
  • So the request reaches the server; run this with Fiddler running a capture and compare request headers with a successful request to see what's up – Alex K. Apr 02 '15 at 15:23
  • @AlexK. We used WireShark but not able to figureout. – Siva Apr 02 '15 at 15:29

1 Answers1

0

This is not a mystery. It is happening because the resource you request was not found.

Either:

  1. The URL you used was incorrect, or
  2. The mapping of URLs to resources at the server isn't correct.
user207421
  • 305,947
  • 44
  • 307
  • 483