libcurl在上传数据之前延迟1秒,命令行curl不会 [英] libcurl delays for 1 second before uploading data, command-line curl does not

查看:614
本文介绍了libcurl在上传数据之前延迟1秒,命令行curl不会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用libcurl向本地服务发送一个API命令(即在127.0.0.1上)。

I am using libcurl to send an API command to a local service (i.e. on 127.0.0.1).

程序用于替换shell脚本使用 curl 程序。)

The program is intended to replace a shell script (that uses the curl program.)

一切正常,除了在某处有1秒延迟,从调用 curl_easy_perform()到我第一次调用读回调函数的时间过去了1秒。

Everything is working, except that there is a 1-second delay somewhere, i.e 1 second elapses from the time I call curl_easy_perform() to when my read callback function is first called.

C程序使用这些选项(错误检查和回调代码省略):

The C program is using these options (error checking & callback code omitted):

curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1:12345/x");
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
curl_easy_setopt(curl, CURLOPT_INFILESIZE, (long)getLengthOfCommandObject());
curl_easy_setopt(curl, CURLOPT_READFUNCTION, &myReadFunction);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &myWriteFunction);

但是如果我从shell中运行 curl 像这样:

But if I run curl from the shell like this:

$ curl --data-binary '<command>' http://127.0.0.1:12345/x

它会立即发送请求,而不会受到1秒的延迟。

it sends the request immediately, without suffering from the 1-second delay.

可能导致延迟的原因是什么,是否有可以设置的选项来阻止它?

What might be causing the delay, and is there an option I can set to prevent it?

修改服务器基于 mongoose

推荐答案

延迟的原因是:

  • libcurl was sending an Expect: 100-Continue header
  • The server (which is based on mongoose) is not configured to send 100 Continue responses automatically.
  • libcurl waits up to 1 second for this response. If it does not receive it after this time then it proceeds to send the request body anyway.

客户端的解决方案是:禁用 Expect 头,如下所示:

A solution on the client side is to disable the Expect header like so:

headers = curl_slist_append(NULL, "Expect:");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
// ...
result = curl_easy_perform(curl);
curl_slist_free_all(headers);

PHP客户端的等效修复相关的PHP问题

这篇关于libcurl在上传数据之前延迟1秒,命令行curl不会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆