c++ - winhttp 和 Visual Studio 代码 - 构建时完成 newb 和“未定义引用”问题
问题描述
我有大约 20 年的 php、js、css、html、xml 等(所有网络内容),但在 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, 0, &str[0], (int)str.size(), 0, 0);
std::wstring res(sz, 0);
MultiByteToWideChar(codepage, 0, &str[0], (int)str.size(), &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,
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::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<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.
解决方案
推荐阅读
- git - git:从 repo 签出后,文件立即显示为已修改
- delphi - 为什么从 winppla.dll 调用函数 A_GetUSBBufferLen() 在 Delphi Tokyo 上不起作用?
- javascript - 以编程方式在内部文本中添加一个带有锚点的跨度标签
- xcode - 产品名称更改未反映在 Xcode 9.4.1 (9F2000) 的项目导航器中
- git - 恢复之前推送到 GitHub 的已删除分支
- swift - 由于“内部”保护级别,无法访问初始化程序
- java - 将字典放入带有重复单词的 HashMap 问题中
- c++ - 从模型中获取下一个项目
- php - 数据库中的 Laravel 缓存冲突
- actions-on-google - 使用 ActionsOnGoogle 和 DialogFlow 从用户那里获得许多有效数字的最佳方法