微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

winhttp 和 Visual Studio 代码 - 构建时完整的 newb 和“未定义引用”问题

如何解决winhttp 和 Visual Studio 代码 - 构建时完整的 newb 和“未定义引用”问题

我有大约 20 年的 PHP、js、css、html、xml 等(所有 Web 内容),但在 C++ 中什么都没有 - 在这里完全是新手。我快速掌握代码,但最好使用我可以“玩”来更好地理解的工作示例。

我安装了 VS Code,安装了 c/c++ 扩展和 MinGW-x64 编译器(我一直在关注 https://code.visualstudio.com/docs/languages/cpp。我还安装了最新的 Windows SDK。

我正在尝试使用下面的示例,我相信我在 SO 上找到了它,它对 PHP 脚本进行了简单的“POST”测试。我相信这对大多数人来说是可笑的,而且事情是错的,但现在这对我来说都是陌生的,除了纯粹通过了解其他语言对正在发生的事情有基本的了解。任何帮助将不胜感激。

#include <Windows.h>
#include <WinHttp.h>
#include <stdio.h>
#include <iostream> //getchar
#include <fstream>

#pragma comment(lib,"winhttp.lib")

using namespace std;

std::wstring get_utf16(const std::string &str,int codepage)
{
    if (str.empty()) return std::wstring();
    int sz = MultiBytetoWideChar(codepage,&str[0],(int)str.size(),0);
    std::wstring res(sz,0);
    MultiBytetoWideChar(codepage,&res[0],sz);
    return res;
}

string HttpsWebRequestPost(string domain,string url,string dat)
{
    //Extra
    LPSTR  data = const_cast<char *>(dat.c_str());;
    DWORD data_len = strlen(data);

    wstring sdomain = get_utf16(domain,CP_UTF8);
    wstring surl = get_utf16(url,CP_UTF8);
    string response;

    DWORD dwSize = 0;
    DWORD dwDownloaded = 0;
    LPSTR pszOutBuffer;
    BOOL  bResults = FALSE;
    HINTERNET  hSession = NULL,hConnect = NULL,hRequest = NULL;
    LPCWSTR additionalHeaders = L"Content-Type: application/x-www-form-urlencoded\r\n";
    DWORD headersLength = -1;

    // Use WinHttpOpen to obtain a session handle.
    hSession = WinHttpOpen(L"WinHTTP Example/1.0",WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,WINHTTP_NO_PROXY_NAME,WINHTTP_NO_PROXY_BYPASS,0);

    // Specify an HTTP server.
    if (hSession)
        hConnect = WinHttpConnect(hSession,sdomain.c_str(),INTERNET_DEFAULT_HTTP_PORT,0);

    // Create an HTTP request handle.
    if (hConnect)
        hRequest = WinHttpOpenRequest(hConnect,L"POST",surl.c_str(),NULL,WINHTTP_NO_REFERER,WINHTTP_DEFAULT_ACCEPT_TYPES,0);

    // Send a request.
    if (hRequest)

        bResults = WinHttpSendRequest(hRequest,additionalHeaders,headersLength,(LPVOID)data,data_len,0);

    // End the request.
    if (bResults)
        bResults = WinHttpReceiveResponse(hRequest,NULL);

    // Keep checking for data until there is nothing left.
    if (bResults)
    {
        do
        {
            // Check for available data.
            dwSize = 0;
            if (!WinHttpQueryDataAvailable(hRequest,&dwSize))
                printf("Error %u in WinHttpQueryDataAvailable.\n",GetLastError());

            // Allocate space for the buffer.
            pszOutBuffer = new char[dwSize + 1];
            if (!pszOutBuffer)
            {
                printf("Out of memory\n");
                dwSize = 0;
            }
            else
            {
                // Read the data.
                ZeroMemory(pszOutBuffer,dwSize + 1);

                if (!WinHttpReadData(hRequest,(LPVOID)pszOutBuffer,dwSize,&dwDownloaded))
                    printf("Error %u in WinHttpReadData.\n",GetLastError());
                else
                    //printf("%s",pszOutBuffer);
                    response = response + string(pszOutBuffer);
                // Free the memory allocated to the buffer.
                delete[] pszOutBuffer;
            }
        } while (dwSize > 0);
    }

    // Report any errors.
    if (!bResults)
        printf("Error %d has occurred.\n",GetLastError());

    // Close any open handles.
    if (hRequest) WinHttpCloseHandle(hRequest);
    if (hConnect) WinHttpCloseHandle(hConnect);
    if (hSession) WinHttpCloseHandle(hSession);

    return response;
}

int main()
{ 
    printf("RESPONSE: %s\n",HttpsWebRequestPost("example.com","/example.PHP","value=1000").c_str());
    system("PAUSE");
}

在构建时,我在控制台中得到以下内容

Starting build...
Build finished with errors(s):
C:\Users\*****\AppData\Local\Temp\cczChaiv.o: In function `HttpsWebRequestPost(std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::__cxx11::basic_string<char,std::allocator<char> >)':
C:/*****/test.cpp:42: undefined reference to `WinHttpOpen'
C:/*****/test.cpp:49: undefined reference to `WinHttpConnect'
C:/*****/test.cpp:54: undefined reference to `WinHttpOpenRequest'
C:/*****/test.cpp:62: undefined reference to `WinHttpSendRequest'
C:/*****/test.cpp:72: undefined reference to `WinHttpReceiveResponse'
C:/*****/test.cpp:81: undefined reference to `WinHttpQueryDataAvailable'
C:/*****/test.cpp:97: undefined reference to `WinHttpReadData'
C:/*****/test.cpp:114: undefined reference to `WinHttpCloseHandle'
C:/*****/test.cpp:115: undefined reference to `WinHttpCloseHandle'
C:/*****/test.cpp:116: undefined reference to `WinHttpCloseHandle'
collect2.exe: error: ld returned 1 exit status

The terminal process terminated with exit code: -1.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。