开发者

How to download a webpage with C

I'm have a bit of a problem with C and XML. Basically, the code I'm using is:

#include <stdio.h>
#include <curl/curl.h>
#include <libxml/tree.h>
#include <string.h>
#include <stdlib.h>

#define WEBPAGE_URL "http://gdata.youtube.com/feeds/api/videos/mKxLmdBzS10"

typedef struct {
 char *contents;
 int size;
} data;

/*Curl uses this function to write the contents of a webpage to a file/stdout*/
size_t write_data( void *ptr, size_t size, size_t nmeb, void *stream)
{
 data *curl_output = (data *)stream;
 int curl_output_size = size * nmeb;

 curl_output->contents = (char *) realloc(curl_output->contents, curl_output->size +     curl_output_size + 1);
 if (curl_output->contents) {
 memcpy(curl_output->contents, ptr, curl_output_size); /*Copying the contents*/
 curl_output->size += curl_output_size;
 curl_output->contents[curl_output->size] = 0;

return curl_output->size;
 }
}

int main()
{

 data webpage;
 webpage.cont开发者_如何学Goents = malloc(1);
 webpage.size = 1;

 CURL *handle = curl_easy_init();
 curl_easy_setopt(handle,CURLOPT_URL,WEBPAGE_URL); /*Using the http protocol*/

 curl_easy_setopt(handle,CURLOPT_WRITEFUNCTION, write_data); /*Setting up the function meant to copy data*/
 curl_easy_setopt(handle,CURLOPT_WRITEDATA, &webpage); /*The data pointer to copy the data*/
 curl_easy_perform(handle);
 curl_easy_cleanup(handle);
 printf("Contents: %s",webpage.contents);
 int i;
}

I'm mean't to be getting this XML back: http://gdata.youtube.com/feeds/api/videos/mKxLmdBzS10.

But currently, I only get arbitrary amounts back, sometimes a third sometimes a half and other times just a quater.

Anybody know what I'm doing wrong?


The problem is in your write_data function.

This line copies the new data to the start of your array, rather than the current end.

memcpy(curl_output->contents, ptr, curl_output_size); /*Copying the contents*/

You need to offset your pointer:

memcpy(curl_output->contents + curl_output->size, ptr, curl_output_size); /* Copying the contents */

Additionally, your return value is bad -- it should be return(curl_output_size); to indicate success, via the number of bytes actually processed on the call -- and below the bracket, return(0); to show an error.

You might also find it clarifies things if instead of having curl_output->size and curl_output_size you pick more distinct names...perhaps curl_output->len?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜