代码之家  ›  专栏  ›  技术社区  ›  S Andrew

错误:“const wchar_t*”类型的参数与“wchar*”类型的参数不兼容

c++
  •  0
  • S Andrew  · 技术社区  · 5 年前

    我有以下代码 WinHTTPRequest 以下内容:

    DWORD WinHTTPRequest(LPCTSTR pServerName, LPCTSTR pRequest, WCHAR* sCommand, LPVOID pPostData, int nPostDataLength, LPCWSTR pwszHeaders, char **dataOut, int *nRead, WCHAR **dataHeaderOut, BOOL bTestProxy, BOOL bSecure, WCHAR* wsRedirect, DWORD *dwReturnStatus)
    {
        HINTERNET hCurrentOpen = NULL;
        if (bTestProxy)
        {
            WCHAR sProxy[255] = L"";
            GetProxy(sProxy);
            if (lstrcmp(sProxy, L"") == 0)
                hCurrentOpen = hOpen;
            else if (lstrcmp(sProxy, g_wsCurrentProxy) != 0)
            {
                if (hOpenProxy)
                    WinHttpCloseHandle(hOpenProxy);
                hOpenProxy = WinHttpOpen(L"Test", WINHTTP_ACCESS_TYPE_NAMED_PROXY, sProxy, NULL, 0/*INTERNET_FLAG_ASYNC*/);
                lstrcpy(g_wsCurrentProxy, sProxy);
                hCurrentOpen = hOpenProxy;
            }
            else
                hCurrentOpen = hOpenProxy;
        }
        else
            hCurrentOpen = hOpen;
    
        HINTERNET hConnect = NULL;
        if (bSecure)
            hConnect = WinHttpConnect(hCurrentOpen, pServerName, INTERNET_DEFAULT_HTTPS_PORT, 0);
        else
            hConnect = WinHttpConnect(hCurrentOpen, pServerName, INTERNET_DEFAULT_HTTP_PORT, 0);
    
        if (!hConnect)
        {
            DWORD dwError = GetLastError();
            return dwError;
        }
    
        DWORD dwFlags;
        if (bSecure)
            dwFlags = WINHTTP_FLAG_SECURE | WINHTTP_FLAG_REFRESH;
        else
            dwFlags = WINHTTP_FLAG_REFRESH;
    
        HINTERNET hRequest = WinHttpOpenRequest(hConnect, sCommand, pRequest, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, dwFlags);
        if (!hRequest)
        {
            DWORD dwError = GetLastError();
            WinHttpCloseHandle(hConnect);
            return dwError;
        }
    
        WinHttpAddRequestHeaders(hRequest, pwszHeaders, -1, WINHTTP_ADDREQ_FLAG_ADD);
        int nLengthPostData;
        if (nPostDataLength == NULL)
        {
            if (pPostData)
                nLengthPostData = strlen((char*)pPostData);
            else
                nLengthPostData = 0;
        }
        else
            nLengthPostData = nPostDataLength;
    
        BOOL bSuccess;
        if (wsRedirect != NULL)
        {
            DWORD dwOption;
            DWORD dwOptionSize;
            dwOption = WINHTTP_OPTION_REDIRECT_POLICY_NEVER;
            dwOptionSize = sizeof(DWORD);
            bSuccess = WinHttpSetOption(hRequest, WINHTTP_OPTION_REDIRECT_POLICY, (LPVOID)&dwOption, dwOptionSize);
            DWORD dwOptionValue = WINHTTP_DISABLE_REDIRECTS;
            bSuccess = WinHttpSetOption(hRequest, WINHTTP_OPTION_DISABLE_FEATURE, &dwOptionValue, sizeof(dwOptionValue));
        }
        BOOL b = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, pPostData, pPostData == NULL ? 0 : nLengthPostData, nLengthPostData, 0);
        if (!b)
        {
            DWORD dwError = GetLastError();
            WinHttpCloseHandle(hConnect);
            WinHttpCloseHandle(hRequest);
            return dwError;
        }
        WinHttpReceiveResponse(hRequest, NULL);
        DWORD dwStatus = 0;
        DWORD dwStatusSize = sizeof(DWORD);
        if (WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, NULL, &dwStatus, &dwStatusSize, NULL))
        {
            if (HTTP_STATUS_REDIRECT == dwStatus || HTTP_STATUS_MOVED == dwStatus)
            {
                DWORD dwSize;
                WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_LOCATION, WINHTTP_HEADER_NAME_BY_INDEX, NULL, &dwSize, WINHTTP_NO_HEADER_INDEX);
                if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
                    return 500;
                LPWSTR pwsRedirectURL = new WCHAR[dwSize];
                bSuccess = WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_LOCATION, WINHTTP_HEADER_NAME_BY_INDEX, pwsRedirectURL, &dwSize, WINHTTP_NO_HEADER_INDEX);
                if (!bSuccess)
                    return 500;
                if (wsRedirect != NULL)
                    lstrcpy(wsRedirect, pwsRedirectURL);
                if (dwReturnStatus != NULL)
                    *dwReturnStatus = dwStatus;
                delete[] pwsRedirectURL;
            }
            else if (dwStatus != HTTP_STATUS_OK && dwStatus != HTTP_STATUS_BAD_REQUEST && dwStatus != HTTP_STATUS_CREATED)
            {
                DWORD dwError = GetLastError();
                WinHttpCloseHandle(hConnect);
                WinHttpCloseHandle(hRequest);
                if (dwReturnStatus != NULL)
                    *dwReturnStatus = dwStatus;
                return dwError;
            }
        }
        if (dataHeaderOut != NULL)
        {
            DWORD dwSize = 0;
            WCHAR *pOutBuffer = NULL;
            if (!WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_RAW_HEADERS_CRLF, WINHTTP_HEADER_NAME_BY_INDEX, NULL, &dwSize, WINHTTP_NO_HEADER_INDEX))
            {
                DWORD dwErr = GetLastError();
                if (dwErr != ERROR_INSUFFICIENT_BUFFER)
                {
                    DWORD dwError = GetLastError();
                    WinHttpCloseHandle(hConnect);
                    WinHttpCloseHandle(hRequest);
                    return dwError;
                }
            }
            pOutBuffer = new WCHAR[dwSize];
            if (WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_RAW_HEADERS_CRLF, WINHTTP_HEADER_NAME_BY_INDEX, pOutBuffer, &dwSize, WINHTTP_NO_HEADER_INDEX))
            {
                pOutBuffer[dwSize] = '\0';
                *dataHeaderOut = (WCHAR*)pOutBuffer;
            }
            //delete[] pOutBuffer;
        }
    
        char *sReadBuffer = NULL;
        DWORD nTotalRead = 0;
        DWORD nToRead = 0;
        DWORD nBytesRead = 0;
        do {
            if (!WinHttpQueryDataAvailable(hRequest, &nToRead))
                break;
            if (nToRead == 0)
                break;
            sReadBuffer = (char*)((sReadBuffer == NULL) ? malloc(nToRead) : realloc(sReadBuffer, nTotalRead + nToRead + 1));
            if (WinHttpReadData(hRequest, sReadBuffer + nTotalRead, nToRead, &nBytesRead))
            {
                nTotalRead += nBytesRead;
            }
        } while (nToRead > 0);
        if (sReadBuffer != NULL && nTotalRead > 0)
        {
            {
                char *sBuffer = new char[nTotalRead + 1];
                memcpy(sBuffer, sReadBuffer, nTotalRead + 1);
                sBuffer[nTotalRead] = '\0';
                *dataOut = sBuffer;
            }
            free(sReadBuffer);
        }
    
        *nRead = nTotalRead;
        WinHttpCloseHandle(hConnect);
        WinHttpCloseHandle(hRequest);
        return ERROR_SUCCESS;
    }
    

    我将上述函数称为:

    dwReturn = WinHTTPRequest(wsHostName, wsURLPathPost, L"POST", sPostData, NULL, wsAdditionalHeaders, &sHTTPData, &nDataRead, &wsDataHeader, 0, 0, wsRedirect, &dwStatus);
    

    但是在 L"POST" ,它给出了以下错误:

    Error (active)  E0167   argument of type "const wchar_t *" is incompatible with parameter of type "WCHAR *" 
    
    Error   C2664   'DWORD WinHTTPRequest(LPCTSTR,LPCTSTR,WCHAR *,LPVOID,int,LPCWSTR,char **,int *,WCHAR **,BOOL,BOOL,WCHAR *,DWORD *)': cannot convert argument 3 from 'const unsigned short [4]' to 'WCHAR *' 
    

    我试着改变 L“邮政” _T("POST") 但没起作用。我怎么能解决这个问题。谢谢

    1 回复  |  直到 5 年前
        1
  •  0
  •   Igor Tandetnik    5 年前

    你拿 sCommand 作为指向非常量的指针,建议您计划修改它指向的缓冲区。但是您在那里传递了一个字符串文本,它不能被修改。要么设置参数const,要么传递一个可修改的缓冲区。