1

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;
linux developer
  • 821
  • 1
  • 13
  • 34
  • 1
    What limitation are you referring to? If authentication works by hard coding the credentials it will work by providing them dynamically as well. Instead of just dumping libcurl post an SSCCE of how you're setting up and sending the HTTP request and see if the actual problem you're having can be resolved. – Captain Obvlious May 21 '13 at 15:59
  • I think you have a better chance of fixing your problem if you post an SSCCE and an outline of what you're doing and what results you get. – Captain Obvlious May 21 '13 at 16:21
  • This is first time i am hearing sscce can you send a link to post my question there. Thanks. I have already sent an email to curl user distribution list and awaiting a response – linux developer May 21 '13 at 16:31
  • 1
    if you have libcurl problems, you should really use the curl-library list for help and support before giving up. That's where the libcurl-hackers hang out... – Daniel Stenberg May 21 '13 at 16:38
  • 1
    Sorry about that, and welcome to Stackoverflow! I usually include [this](http://sscce.org/) link for Short Self Contained Correct/Compilable Examples (SSCCE). Basically you take a large chunk of code and reduce it a small example that exhibits the behavior you are experiencing. – Captain Obvlious May 21 '13 at 17:38
  • One of the response i got is "libcurl _can_ do that in some limited situations. Like if your libcurl was built with SSPI support and you use NTLM, you can set the "userpwd" to just ":" as a sort of magic hint to libcurl and then it will get and use the user+password of the current user for that HTTP request. " – linux developer May 22 '13 at 22:15

1 Answers1

3

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

Dory Zidon
  • 10,497
  • 2
  • 25
  • 39
  • i tried to run a sample code using winhttp looks like it does not have issue with proxy authentication. I have never user winhttp. But i am more used to libcurl. However if there is a way around to come across proxy authentication, it would be something great!!! – linux developer May 21 '13 at 15:37
  • I didn't say it does, I only said that WinINet is simpler :) look at the compare link I sent you.. – Dory Zidon May 21 '13 at 15:38