Datei per HTTPS/SSL downloaden?



  • Ich habe eine kleine Dialogapplikation mit einem Editfeld geschrieben, in das der Inhalt einer Datei geschrieben werden soll, welche aus dem Web (www.server.de/test.txt) geholt wird. Dies funktioniert unverschlüsselt bereits:

    CString server = "www.server.de";
    CString path = "/test.txt";
    INTERNET_PORT port = 80;
    //INTERNET_PORT port = 443; // (1)
    
    CHttpFile *file;
    CInternetSession *session = new CInternetSession();
    CHttpConnection *connection = session->GetHttpConnection(server, port);
    //CHttpConnection *connection = session->GetHttpConnection(server, INTERNET_FLAG_SECURE, port); // (2)
    
    file = connection->OpenRequest(1, path);
    //file = connection->OpenRequest(1, path, NULL, 1, NULL, NULL, INTERNET_FLAG_SECURE); // (3)
    file->SendRequest();
    
    char chr[2];
    while (file->Read(chr, 1)) {
    	chr[1] = '\0';
    	m_Edit += chr;
    }
    m_Edit.Replace("\n","\r\n");
    UpdateData(false);
    
    delete session;
    

    Ich habe (1), (2) und (3) bereits versucht. Die Fehler sind:

    (1) an, (2)+(3) aus: Download der Error 400 Page
    (1)+(2) an, (3) aus: Download der Error 400 Page
    (1)+(2)+(3) an: Fehlermeldung "Die Zertifizierungsagentur ist ungültig oder fehlerhaft"
    (1) aus, (2)+(3) an: Fehlermeldung "Im Support des sicheren Channels ist ein Fehler aufgetreten"
    (1)+(2) aus, (3) an: Fehlermeldung "Im Support des sicheren Channels ist ein Fehler aufgetreten"
    (1)+(3) aus, (2) an: Download der Datei (jedoch nicht über HTTPS)

    Ich bin ratlos. Kann mir vielleicht einer zur Hand gehen? Danke!



  • Hat denn hier noch keiner mit HTTPS gearbeitet? 😞



  • Gibts denn da keine Funktion in der MSDN, die auch einigermaßen erklärt ist?



  • Wenn ich eine gefunden hätte, würde ich nicht fragen. :p



  • Mein bisheriger Code:

    HINTERNET hOpen, hConnect, hURL;
    CString strAgent("WhatSoEver");
    BOOL bSend;
    
    // Initiate the connection
    hOpen = InternetOpen(strAgent, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, NULL);
    if (!hOpen) {
    	AfxMessageBox(_T("InternetOpen() failed."), MB_ICONSTOP);
    	return;
    }
    
    // Open the connection
    hConnect = InternetConnect(hOpen, _T("www.server.de"), 443, NULL, NULL, INTERNET_SERVICE_HTTP, NULL, NULL);
    if (!hConnect) {
    	AfxMessageBox(_T("InternetConnect() failed."), MB_ICONSTOP);
    	InternetCloseHandle(hOpen);
    	return;
    }
    
    // Create the HTTP request
    hURL = HttpOpenRequest(hConnect, NULL, _T("/example.file"), NULL, NULL, NULL, INTERNET_FLAG_SECURE, NULL);
    if (!hConnect) {
    	AfxMessageBox(_T("HttpOpenRequest() failed."), MB_ICONSTOP);
    	InternetCloseHandle(hConnect);
    	InternetCloseHandle(hOpen);
    	return;
    }
    
    // Ignore invalid certificate authorities
    DWORD dwFlags, dwSize;
    dwSize = sizeof(dwFlags);
    InternetQueryOption(hURL, INTERNET_OPTION_SECURITY_FLAGS, (LPVOID)&dwFlags, &dwSize);
    dwFlags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;
    InternetSetOption(hURL, INTERNET_OPTION_SECURITY_FLAGS, &dwFlags, sizeof(dwSize));
    
    // Send the request
    bSend = HttpSendRequest(hURL, NULL, NULL, NULL, NULL);
    if (!bSend) {
    	AfxMessageBox(_T("HttpSendRequest() failed."), MB_ICONSTOP);
    	InternetCloseHandle(hConnect);
    	InternetCloseHandle(hOpen);
    	return;
    } else {
    	AfxMessageBox(_T("Connection OK."), MB_ICONSTOP);
    }
    
    // Close all handles
    InternetCloseHandle(hURL);
    InternetCloseHandle(hConnect);
    InternetCloseHandle(hOpen);
    

    Die Funktion endet bei der Fehlermeldung "HttpSendRequest() failed. Code 12055" (ERROR_INTERNET_SEC_CERT_ERRORS). Ideen?


Anmelden zum Antworten