简体   繁体   English

C ++ cURL每次都是“来自服务器的空回复”

[英]C++ cURL “empty reply from server” every time

I am trying to write a C++ program to read from a website using cURL. 我正在尝试编写一个C ++程序,使用cURL从网站上读取。 I have started with using the code from the cURL example simple.c. 我已经开始使用cURL示例simple.c中的代码了。 If I use that code exactly, no matter what URL I try to access, I obtain error #52: "empty reply from server". 如果我完全使用该代码,无论我尝试访问哪个URL,我都会收到错误#52:“来自服务器的空回复”。 This is for any URL I try. 这适用于我尝试的任何网址。 If I use cURL from the command line, it seems to work fine. 如果我从命令行使用cURL,它似乎工作正常。

The verbose response of my program is: 我的程序的冗长响应是:

*About to connect() to www.google.com port 80 (#0)
* Trying 74.125.91.99...* connected
*connected to www.google.com (74.125.91.99) port 80 (#0)
>GET / HTTP/1.1
Host: www.google.com
Accept: */*

* Empty reply from server
* Connection #0 to host www.google.com left intact
* server returned nothing (no headers, no data)
*Closing connection #0

The code is: 代码是:

CURL *curl; 
CURLcode res;

curl = curl_easy_init(); 
curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com"); 
curl_easy_setopt(curl, CURLOPT_VERBOSE, TRUE);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl); 

I lost a couple hours debugging an empty response from curl, so I'm sharing here to hopefully prevent the same from happening to others. 我失去了几个小时调试curl的空响应,所以我在这里分享希望防止同样的事情发生在其他人身上。

I noticed that OPTIONS calls were returned with the "Empty reply from server" error in both the browser and from curl on my machine, but worked fine from a colleague's. 我注意到OPTIONS调用在浏览器和我的机器上的卷曲中都返回了“从服务器清空回复”错误,但是从同事那里工作得很好。 When I tried to telnet to port 80 on the web server, the connection would immediately close as soon as I hit ENTER after typing the "OPTIONS /path/to/resource HTTP/1.1" line. 当我尝试telnet到Web服务器上的端口80时,只要在键入“OPTIONS / path / to / resource HTTP / 1.1”行后按Enter键,连接就会立即关闭。 Doing an "lsof -i | grep telnet" (with the telnet session open, before typing the OPTIONS line), I saw that my telnet session was to something local, NOT the server I'd specified. 做一个“lsof -i | grep telnet”(打开telnet会话,在输入OPTIONS行之前),我看到我的telnet会话是本地的,而不是我指定的服务器。 I knew something REALLY strange was going on then. 我知道当时发生了一些非常奇怪的事情。 Turns out it was because I had Cisco AnyConnect VPN software installed. 事实证明这是因为我安装了Cisco AnyConnect VPN软件。 The VPN need not be running--merely having it installed was enough for it in intercept my traffic. VPN无需运行 - 仅安装它就足以拦截我的流量。 Nasty. 讨厌。 A little googling turned up this page, which talks more about the issue: 一个小小的谷歌搜索出现在这个页面,它更多地讨论了这个问题:

http://www.bennadel.com/blog/2559-cisco-anyconnect-vpn-client-may-block-cors-ajax-options-requests.htm http://www.bennadel.com/blog/2559-cisco-anyconnect-vpn-client-may-block-cors-ajax-options-requests.htm

So, short story is: it might not be the server...something local could be intercepting and blocking your network traffic. 因此,简短的故事是:它可能不是服务器......本地可能会拦截并阻止您的网络流量。

"Empty reply from server" means that libcurl didn't receive any response from the server after it sent off its request. “来自服务器的空回复”表示libcurl在发送其请求后未收到服务器的任何响应。 In HTTP every request always gets something back so this is not even responding with HTTP and is thus a rather sever error. 在HTTP中,每个请求总会得到一些回复,所以这甚至没有响应HTTP,因此是一个相当严重的错误。

You use a very old libcurl so possibly you're experiencing a bug that existed back then. 你使用一个非常古老的libcurl,所以你可能遇到了当时存在的bug。

Other possible reasons for this is that something in your network/setup is preventing this from working, like a firewall. 其他可能的原因是网络/设置中的某些内容阻止了它的工作,如防火墙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM