libcurl HTTP HEAD 请求没有自动代理连接标头

发布于 2024-09-04 15:00:20 字数 1468 浏览 8 评论 0原文

我需要关于使用 libcurl 进行以下操作的说明:

我需要发送如下所示的 http HEAD 请求 ::

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

我编写的代码(如下所示)以稍微不同的方式发送 HEAD 请求:

 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();

上面显示的代码发送 HEAD 请求形式略有不同,添加一个 Proxy-Connection 标头(如下所示)

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

任何人都可以分享适当的代码吗?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒路情人 2024-09-11 15:00:20

这是因为 libcurl 似乎自动读取环境变量 http_proxy 造成的。当您位于 HTTP 代理后面时,此请求是正常的。代理需要知道完整的 URL 才能发出请求,这就是请求看起来不同的原因。

在执行程序之前尝试取消设置环境变量 http_proxy (export http_proxy=""),请求将按预期发出。

我相信有一种方法可以明确告诉 libcurl 使用/不使用代理,但我不记得了。

顺便说一句,这两行代码

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

没有意义,因为没有发送空标头。

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.

灰色世界里的红玫瑰 2024-09-11 15:00:20

如果您不想使用 http_proxy 环境,请通过空值调用函数,如下所示。

curl_easy_setopt(curl, CURLOPT_PROXY, "");

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

curl_easy_setopt(curl, CURLOPT_PROXY, "");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文