我的应用程序在 Windows 中运行,我必须向服务器发送一些请求。我目前正在使用 libcurl。
但是,在请求发送到服务器之前涉及具有身份验证的代理服务器时存在限制。
我探索了 libcurl 中的选项,但我无法使其工作。如果我对用户名/密码进行硬编码,它就可以工作。但我不想通过我的应用程序获取用户名和密码;我希望 libcurl 可以在传输开始时请求这些。
有没有我可以使用的不同库,比如 winhttp 或 libcurl 是最好用的库?
以下是在发出请求之前完成的 curl 设置
curlResultCode = curl_easy_setopt(curlHandle, CURLOPT_URL, urlString.data());
curlResultCode = curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, receiveDataCallback);
curlResultCode = curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, &curlPerformStorage);
curlResultCode = curl_easy_setopt(curlHandle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
curlResultCode = curl_easy_setopt(curlHandle, CURLOPT_FOLLOWLOCATION, 1);
if(NULL != GetWindowsProxyConfig().lpszProxy){
LPWSTR wStr = GetWindowsProxyConfig().lpszProxy;
char* buffer = new char[100];
wcstombs(buffer, wStr, 100) ;
curlResultCode = curl_easy_setopt(curlHandle, CURLOPT_PROXY, "<proxy_name>");
curlResultCode = curl_easy_setopt(curlHandle, CURLOPT_PROXYPORT, <number>);
curlResultCode = curl_easy_setopt(curlHandle, CURLOPT_HTTPAUTH, CURLAUTH_NONE);
curlResultCode = curl_easy_setopt(curlHandle, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
} else {
curlResultCode = curl_easy_setopt(curlHandle, CURLOPT_PROXY, "<proxy_name>");
curlResultCode = curl_easy_setopt(curlHandle, CURLOPT_PROXYPORT, <number>);
curlResultCode = curl_easy_setopt(curlHandle, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
curlResultCode = curl_easy_setopt(curlHandle, CURLOPT_PROXYAUTH, CURLAUTH_NTLM);
}
curlResultCode = curl_easy_setopt(curlHandle, CURLOPT_USE_SSL, CURLUSESSL_ALL);
curlResultCode = curl_easy_setopt(curlHandle, CURLOPT_SSL_VERIFYPEER, 1);
curlResultCode = curl_easy_setopt(curlHandle, CURLOPT_SSL_VERIFYHOST, 2);
if(proxyStr.length() > 0){
curlResultCode = curl_easy_setopt(curlHandle, CURLOPT_HTTPPROXYTUNNEL, 1L);
}
curlResultCode = curl_easy_setopt(curlHandle, CURLOPT_FAILONERROR, 1);
curlResultCode= curl_easy_setopt ( curlHandle, CURLOPT_ERRORBUFFER, errorbuffer );
int nConnectionTimeout = curlPerformStorage.GetConnectionTimeOut();
if (nConnectionTimeout > 0)
{
curlResultCode = curl_easy_setopt(curlHandle, CURLOPT_CONNECTTIMEOUT, nConnectionTimeout);
}
int nTransferTimeout = curlPerformStorage.GetTransferTimeOut();
if (nTransferTimeout > 0)
{
curlResultCode=curl_easy_setopt(curlHandle, CURLOPT_TIMEOUT, nTransferTimeout);
}
curlResultCode = curl_easy_setopt(curlHandle, CURLOPT_PROGRESSFUNCTION , ProgressCallback);
if (curlResultCode == CURLE_OK)
{
curlResultCode = curl_easy_setopt(curlHandle, CURLOPT_NOPROGRESS, FALSE);
curlResultCode= curl_easy_setopt(curlHandle, CURLOPT_PROGRESSDATA , &curlPerformStorage);
}
curlResultCode = curl_easy_perform(curlHandle);
if (curlResultCode != CURLE_OK)
{
}
else
{
}
curl_easy_reset(curlHandle);
return curlResultCode;