libcurl HTTP HEAD request without automatic Proxy-

2019-05-18 19:07发布

问题:

I need clarifications on using libcurl for the following:

I need to send an http HEAD request shown as below ::

HEAD /mshare/3/30002:12:primary/stream_xNKNVH.mpeg HTTP/1.1
Host: 192.168.70.1:8080
Accept: */*
User-Agent: Kreatel_IP-STB
getcontentFeatures.dlna.org: 1

The code I wrote (shown below) , sends the HEAD Request in slightly different way:

 curl_global_init(CURL_GLOBAL_ALL);
 CURL* ctx = NULL;
 const char *url = "http://192.168.70.1:8080/mshare/3/30002:12:primary/stream_xNKNVH.mpeg" ;
 char *returnString;
 struct curl_slist *headers = NULL;
 ctx = curl_easy_init();
 headers = curl_slist_append(headers,"Accept: */*");
 headers = curl_slist_append(headers,"User-Agent: Kreatel_IP-STB");\
 headers = curl_slist_append(headers,"getcontentFeatures.dlna.org: 1");
 headers = curl_slist_append(headers,"Pragma:");
 headers = curl_slist_append(headers,"Proxy-Connection:");
 curl_easy_setopt(ctx,CURLOPT_HTTPHEADER , headers );
 curl_easy_setopt(ctx,CURLOPT_NOBODY ,1 );
 curl_easy_setopt(ctx,CURLOPT_VERBOSE, 1);
 curl_easy_setopt(ctx,CURLOPT_URL,url );
 curl_easy_setopt(ctx,CURLOPT_NOPROGRESS ,1 );
 curl_easy_perform(ctx);
 curl_easy_cleanup(ctx);
 curl_global_cleanup();

The code shown above sends the HEAD Request in slightly different form, adding a Proxy-Connection header (shown below)

HEAD http://192.168.70.1:8080/mshare/3/30002:12:primary/stream_xNKNVH.mpeg HTTP/1.1
Host: 192.168.70.1:8080
Proxy-Connection: Keep-Alive
Accept: */*
User-Agent: Kreatel_IP-STB
getcontentFeatures.dlna.org: 1

Can any one , share the appropriate code ?

回答1:

This is caused because libcurl seems to automagically read the environmental variable http_proxy. This request is normal when you are behind a HTTP Proxy. The proxy needs to know the full URL in order to make the request and this is why the request looks different.

Try to unset your environmental variable http_proxy (export http_proxy="") before executing your program and the request will be made as expected.

I believe there is a way to explicitly tell libcurl to use/don't use a proxy, but I don't remember it.

By the way, this two lines of code

 headers = curl_slist_append(headers,"Pragma:");
 headers = curl_slist_append(headers,"Proxy-Connection:");

are meaningless because empty headers are not sent.



回答2:

if you don't want to use http_proxy envirionment, call function by empty value like below.

curl_easy_setopt(curl, CURLOPT_PROXY, "");


标签: c libcurl