如何用C语言下载网页
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题出在您的 write_data 函数中。
此行将新数据复制到数组的开头,而不是当前末尾。
您需要偏移您的指针:
此外,您的返回值很糟糕 - 它应该是
return(curl_output_size);
通过调用中实际处理的字节数来指示成功 - 并且低于括号,return(0);
显示错误。如果您选择更多不同的名称而不是
curl_output->size
和curl_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.
You need to offset your pointer:
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
andcurl_output_size
you pick more distinct names...perhapscurl_output->len
?