如何用C语言下载网页

发布于 2024-11-15 18:05:28 字数 1662 浏览 2 评论 0原文

我对 C 和 XML 有一点疑问。 基本上,我使用的代码是:

#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.contents = 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;
}

我不想取回此 XML: http://gdata.youtube.com/feeds/api/videos/mKxLmdBzS10

但目前,我只能拿回任意金额,有时是三分之一,有时是一半,有时只是四分之一。

有人知道我做错了什么吗?

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.contents = 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?

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

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

发布评论

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

评论(1

笑梦风尘 2024-11-22 18:05:28

问题出在您的 write_data 函数中。

此行将新数据复制到数组的开头,而不是当前末尾。

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

您需要偏移您的指针:

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

此外,您的返回值很糟糕 - 它应该是 return(curl_output_size); 通过调用中实际处理的字节数来指示成功 - 并且低于括号,return(0); 显示错误。

如果您选择更多不同的名称而不是 curl_output->sizecurl_output_size,您可能还会发现它会澄清一些事情......也许 curl_output->len< /代码>?

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?

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