【问题标题】:Can Curl read only first 2000 bytes?Curl 只能读取前 2000 个字节吗?
【发布时间】:2011-06-24 17:37:50
【问题描述】:

我使用 curl 和 char URL[]= "file:///d:/temporal/prueba1.txt" 来测试使用硬盘驱动器文件来测试接收到的数据。

我有以下问题:

  1. 跳过大文件中的前 2 个字符:-已解决- 读取数据时出错
  2. 我只想读取一次(最好是前 2000 个字符)是否存在 curl 命令来执行此操作?
  3. 如果我在 writer 文件中进行: long longitud=strlen(datain),我得到的经度远大于 size*nmemb
  4. Curl 不能通过企业网络,但是 URLOpenBlockingStream() + read() 可以做到
  5. 有时 Curl 从 url 回读大量错误数据。我认为发生了连接错误,但我不明白

这是程序。 (注意:pull_one_url() 是使用线程调用的):

static void *pull_one_url(void *ii){
ix_bloque=initialize();
     lee_curl_c *lee_curl;//Curl class
CURL *curl; 
CURLcode result; 
curl = curl_easy_init(); // Create our curl handle 
char errorBuffer[CURL_ERROR_SIZE]=""; 
char user_agent[]="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
// Write all expected data in here 

if (curl) 
{ 
    curl_easy_setopt(curl, CURLOPT_USERAGENT,user_agent);
    curl_easy_setopt(curl, CURLOPT_PORT, PC_PORT_a); // Check this before using 
    curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer); 
    curl_easy_setopt(curl, CURLOPT_URL, lee_curl->url[ix_bloque]); 
    curl_easy_setopt(curl, CURLOPT_HEADER, 0); 
    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, D_TIMEOUT);//240 segundos
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0);//SOLO primer http, ignorar sucesivos
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &writer); 
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, lee_curl->buffer[ix_bloque]); 
    curl_easy_setopt(curl, CURLOPT_VERBOSE, VERBOSE_url);//Para ver que esta pasando
    // Attempt to retrieve the remote page  
    result = curl_easy_perform(curl); 
    if (errorBuffer[0])
    {
        fprintf(stderr, "\n##Tarea %li ERROR EN    %s DEBIDO A: %s",ix_bloque,lee_curl->url[ix_bloque],errorBuffer);
        lee_curl->estado[ix_bloque]=2;//error
    }
    else
    {
        fprintf(stderr, "\n##Tarea %li NO ERROR EN %s          ",ix_bloque,lee_curl->url[ix_bloque]);
        lee_curl->estado[ix_bloque]=1;//Lleno
    }
    // Always cleanup 
    curl_easy_cleanup(curl); 
} 
return NULL;    }

这里是编写器函数:

static size_t writer(void *vdata, size_t size, size_t nmemb, void *vbuffer_in){ 
char *datain=(char *) vdata;
char *buffer_in=(char *) vbuffer_in;
nmemb*=size;//No multiplication needed anymore
//long longitud=strlen(datain);//longitud always larger than nmemb 
// Is there anything in the buffer? 
if ( (buffer_in != NULL) && (buffer_in[0]=='\0') ) //CAUTION, reads only if buffer is empty!!!
{ 
    //strcat(buffer_in,data); //SIRVE si el fichero es de mas de 16kb OJO!
    if (nmemb<(D_BUFFER_SIZE-1))
    {
        memcpy(buffer_in,datain,nmemb);
        buffer_in[nmemb]='\0';//end of string
        fprintf(stderr,"\n###SUCCESSFUL, loaded :      %li bytes",nmemb);
    }
    else
    {
        memcpy(buffer_in,data,D_BUFFER_SIZE);
        buffer_in[D_BUFFER_SIZE-1]='\0';//end of string
        fprintf(stderr,"\n###WARNING, trying to load   %li LARGER THAN %li",nmemb,D_BUFFER_SIZE);
        fprintf(stderr," El tamano maximo curl=%li = %likB",CURL_MAX_WRITE_SIZE,CURL_MAX_WRITE_SIZE/1024);
    }
return nmemb; 
} 
return 0;     }

【问题讨论】:

    标签: php c++ c curl


    【解决方案1】:

    CURLOPT_RANGE 允许您请求文件/资源​​的一部分:

    http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTRANGE

    【讨论】:

    • 谢谢。我发现 curl 将 ASCII: "0D 0A" 更改为 "0D 0d 0A"
    • 好的!你是对的丹尼尔。如果使用 fprintf 在文件中写入时会出现问题,该问题会在回车前添加 0x0d。所以我不得不以二进制模式打开:fopen("output.txt","wb")@Daniel Stenberg
    猜你喜欢
    • 2013-06-23
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多