如何使用MFC C ++作为客户端在api.imgur.com上上传图像

时间:2018-10-30 12:19:18

标签: c++ winapi file-upload imgur

我制作了用于在api.imgur.com/3/image上上传图像的C ++客户端。

我成功建立了连接并获得了响应,但是当我将图像作为字节数组发送时,我得到的响应就是

  

“无法上传”

我不知道该发送什么。请提出建议。

char *pszFormHeader = "------Boundary%08X\r\nContent-Disposition: form-data; \r\nContent-Type: application/x-www-form-urlencoded image/jpg text/xml, text/plain\r\n\r\n";
char *pszFinalBoundary = "\r\n------Boundary%08X--\r\n";
wchar_t *wszContentHeader = L"Authorization: Client-ID 0a50102b339d40b";///*boundary=----Boundary%08X; Content-Type: multipart/form-data;*/
wchar_t wszContentLength[256] = { 0 };

#define BUFFER_SIZE_1KB 1024

BOOL UploadFileToServer(wchar_t *wszURL, wchar_t *wszFilePath, wchar_t *_wszProxyAddress, wchar_t *_wszServerAddress);

int main(int argc, char **argv)
{
    UploadFileToServer(L"3/upload", L"C:\\Users\\absingh\\Desktop\\2.jpg", NULL, L"api.imgur.com");
    //UploadFileToServer(L"upload.php", L"c:\\sample2.txt", NULL, L"www.example.com");

}

BOOL UploadFileToServer(wchar_t *wszURL, wchar_t *wszFilePath, wchar_t *_wszProxyAddress, wchar_t *_wszServerAddress)
{
    /*
    * Declarations and initializations
    */

    try
    {


    HINTERNET  hSession = NULL, hConnect = NULL, hRequest = NULL;
    BOOL bResults;
    DWORD dwSize = 0;
    DWORD dwContentLength = 0;
    DWORD dwBytesToRead = 0;
    DWORD dwBytesRead = 0;
    DWORD dwBytesWritten = 0;
    DWORD dwBoundaryValue = 0;
    DWORD dwFileSize = 0;
    DWORD options = SECURITY_FLAG_IGNORE_CERT_CN_INVALID | SECURITY_FLAG_IGNORE_CERT_DATE_INVALID | SECURITY_FLAG_IGNORE_UNKNOWN_CA;
    LPCWSTR wszProxyAddress = 0;
    wchar_t wszContentLength[256] = { 0 };

    PUCHAR pResponse = 0;
    PCHAR pFormHeader = 0;
    PCHAR pFinalBoundary = 0;
    PUCHAR pBuf = 0;
    wchar_t *pContentHeader = 0;

    /*
    * Preparations, set up the content headers
    */
    pFormHeader = (PCHAR)calloc(BUFFER_SIZE_1KB, 1);
    pFinalBoundary = (PCHAR)calloc(BUFFER_SIZE_1KB, 1);
    pContentHeader = (wchar_t *)calloc(BUFFER_SIZE_1KB, 1);

    dwBoundaryValue = GetTickCount();
    wszProxyAddress = _wszProxyAddress;

    sprintf_s(pFormHeader, BUFFER_SIZE_1KB, pszFormHeader, dwBoundaryValue, dwBoundaryValue);
    sprintf_s(pFinalBoundary, BUFFER_SIZE_1KB, pszFinalBoundary, dwBoundaryValue);
    wsprintf(pContentHeader, wszContentHeader, dwBoundaryValue);

    if (wszProxyAddress != NULL && wcslen(wszProxyAddress) < 4)
    {
        wszProxyAddress = NULL;
    }

    HANDLE hFile = CreateFile(wszFilePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);

    if (hFile == INVALID_HANDLE_VALUE)
    {
        free(pFormHeader);
        free(pFinalBoundary);
        free(pContentHeader);
        printf("Unable to open the file for sending, aborting...\r\n");
        return FALSE;
    }

    dwFileSize = GetFileSize(hFile, NULL);

    if (dwFileSize == 0)
    {
        free(pFormHeader);
        free(pFinalBoundary);
        free(pContentHeader);
        printf("The file is 0 bytes in size, cannot send it\r\n");
        return FALSE;
    }

    if (wszProxyAddress == NULL)
    {
        hSession = WinHttpOpen(L"Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko",
            WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
            WINHTTP_NO_PROXY_NAME,
            WINHTTP_NO_PROXY_BYPASS, 0);
    }
    else
    {
        hSession = WinHttpOpen(L"Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko",
            WINHTTP_ACCESS_TYPE_NAMED_PROXY,
            wszProxyAddress,
            WINHTTP_NO_PROXY_BYPASS, 0);
    }


    if (hSession)
    {
        hConnect = WinHttpConnect(hSession, _wszServerAddress,
            INTERNET_DEFAULT_HTTPS_PORT, 0);
    }
    else
    {
        free(pFormHeader);
        free(pFinalBoundary);
        free(pContentHeader);
        printf("hSession failed, errorcode 0x%08x\r\n", GetLastError());
        return FALSE;
    }

    if (hConnect)
    {
        hRequest = WinHttpOpenRequest(hConnect, L"POST", wszURL,
            NULL, WINHTTP_NO_REFERER,
            WINHTTP_DEFAULT_ACCEPT_TYPES,
            WINHTTP_FLAG_SECURE | WINHTTP_FLAG_REFRESH);
    }
    else
    {
        free(pFormHeader);
        free(pFinalBoundary);
        free(pContentHeader);
        printf("hConnect failed, errorcode 0x%08x\r\n", GetLastError());
        WinHttpCloseHandle(hSession);
        return FALSE;
    }

    if (hRequest)
    {
        bResults = WinHttpSetOption(hRequest, WINHTTP_OPTION_SECURITY_FLAGS, (LPVOID)&options, sizeof(DWORD));

        WinHttpAddRequestHeaders(hRequest, pContentHeader, (ULONG)-1L, WINHTTP_ADDREQ_FLAG_ADD);
        WinHttpAddRequestHeaders(hRequest, L"Connection: close", (ULONG)-1L, WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_REPLACE);

        dwContentLength = strlen(pFormHeader) + dwFileSize + strlen(pFinalBoundary);

        pBuf = (PUCHAR)calloc(dwContentLength, 1);

        strcat_s((PCHAR)pBuf, dwContentLength, pFormHeader);

        ReadFile(hFile, &pBuf[strlen(pFormHeader)], dwFileSize, &dwBytesRead, NULL);

            memcpy(&pBuf[strlen(pFormHeader) + dwFileSize], pFinalBoundary, strlen(pFinalBoundary));

        wsprintf(wszContentLength, L"Content-Length: %d", dwContentLength);

        bResults = WinHttpSendRequest(hRequest,
            wszContentLength,
            -1, pBuf, dwContentLength,
            dwContentLength, 0);

    }

    }
    }



    }
    else
    {
`enter code here`HEAP_NO_SERIALIZE, pszDestination);
        free(pFormHeader);
        free(pFinalBoundary);
        free(pContentHeader);
        printf("hRequest failed, errorcode 0x%08x\r\n", GetLastError());
        WinHttpCloseHandle(hSession);
        WinHttpCloseHandle(hConnect);
        return FALSE;
    }

    if (bResults)
    {
        bResults = WinHttpReceiveResponse(hRequest, NULL);
    }
    else
    {
        printf("hResults failed, errorcode 0x%08x\r\n");
        WinHttpCloseHandle(hRequest);
        WinHttpCloseHandle(hConnect);
        WinHttpCloseHandle(hSession);
        free(pBuf);
        free(pFormHeader);
        free(pFinalBoundary);
        free(pContentHeader);
        return FALSE;
    }

    WinHttpQueryDataAvailable(hRequest, &dwBytesToRead);

    pResponse = (PUCHAR)calloc(dwBytesToRead, 1);

    WinHttpReadData(hRequest, pResponse, dwBytesToRead, &dwBytesRead);

    free(pResponse);
    free(pFormHeader);
    free(pFinalBoundary);
    free(pContentHeader);
    // Close any open handles.
    if (hRequest) WinHttpCloseHandle(hRequest);
    if (hConnect) WinHttpCloseHandle(hConnect);
    if (hSession) WinHttpCloseHandle(hSession);

    free(pBuf);
    CloseHandle(hFile);

    DeleteFile(wszFilePath);

    }
    catch (...)
    {
        //TCHAR sz[1024];
        //pEx->GetErrorMessage(sz, 1024);
        //pEx->Delete();
    }


    return TRUE;
}

0 个答案:

没有答案