Pregunta

My application is running in Windows and I have to send some requests to the server. I am currently using libcurl.

However there is a limitation when proxy server with authentication is involved before the request sent out to the server.

I explored the options in libcurl but i could not able to make it work. If i hardcode the username / password it works. But I do not want to get the user name and password by my application; I would prefer that libcurl can requests these at the start of a transfer.

Is there a different library i can use like winhttp or libcurl is the best one to use?

Below are the curl settings done before making the request

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;
¿Fue útil?

Solución

I have implemented proxy usages with libcurl, it is possible and is the most cross platform solutions.

Having said that, on Windows your best bet is to stick with the windows comm stacks, as those will be easier to manipulate on windows, and will be easier to work with proxies (many times getting their config from the machine definitions automatically.

On windows its mostly a toss up between WinHTTP and WinINet.

  • WinINet is considered easier and more high level, suitable for clients access mostly. For example it will popup the box asking for a proxy username and password automatically.
  • WinHTTP is considered very low level and more suitable for very complex servers running multithreaded where you need a lot of control over the conneciton.

90% of the time you can just use Wininet

Here is a complete compare between the two: Microsoft Compare of WinHTTP and WinINet

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top