如何像浏览器一样从 C 中的 URL 下载文件? [英] How to download a file from a URL in C, as a browser would?

查看:25
本文介绍了如何像浏览器一样从 C 中的 URL 下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个 URL 下载一个文件,这个:http://download.finance.yahoo/d/quotes.csv?s=YHOO+GOOG+MSFT&f=sl1d1t1c1hgvbap2当我打开浏览器并在浏览器中输入此 URL 时,文件会自动下载.我想要的是使用 C 语言程序在不使用浏览器的情况下下载此文件,我需要金融项目的此类信息.我尝试使用 libcurl 下载文件,但 libcurl 下载了与此 URL 相对应的 HTML 页面,该页面当然是空的,因为此 URL 唯一能做的就是开始下载.我想这个 URL 是某种 HTTP 服务器的方式,但我完全不知道如何获取这个文件.

I want to download a file from a URL, this one : http://download.finance.yahoo/d/quotes.csv?s=YHOO+GOOG+MSFT&f=sl1d1t1c1hgvbap2 When i go on my browser and input this URL in my browser, the file is automatically downloaded as it should. What I want is to download this file without going on my browser using a program in C language, I need this type of information for a financial project. I tried to download the file using libcurl but libcurl downloads the HTML page corresponding to this URL which is empty of course because the only thing this URL does is start a download. I suppose this URL is the way int a HTTP server of some kind but I am completely lost as to how to get this file.

在此先感谢大家的时间和帮助,如果您能通过解释或更好的 C 代码提供帮助,请随时这样做,不要害怕过于精确.

Thank you all in advance for your time and help, please if you can help by explaining or even better with C code feel free to do so and don't be afraid to be too precise.

推荐答案

使用 libcurl,参见 此示例页面.

如果你想让它工作,也让它与命令行 curl 一起工作,并使用 --libcurl 选项.我怀疑问题可能更多地与 javascript、cookies、登录或其他任何东西有关.所有这些都是可解决的,但请使用命令行使其工作.我的诊断是您的 URL 在 yahoo 之后缺少 .com.

If you want to get it to work, make it work with the command line curl too, and use the --libcurl option. I suspect the problem might be more to do with javascript, cookies, a login or whatever. All these are soluble, but play with the command line to get it to work. My diagnosis here is that your URL is missing .com after yahoo.

例如:

curl --silent --libcurl /tmp/test.c 'http://download.finance.yahoo.com/d/quotes.csv?s=YHOO+GOOG+MSFT&f=sl1d1t1c1hgvbap2'

产生屏幕输出:

"YHOO",51.04,"11/21/2014","4:00pm",-0.21,52.25,50.99,22226984,N/A,52.49,"-0.41%"
"GOOG",537.50,"11/21/2014","4:00pm",+2.67,542.14,536.56,2218249,N/A,575.00,"+0.50%"
"MSFT",47.98,"11/21/2014","4:00pm",-0.72,49.05,47.57,42884796,N/A,49.05,"-1.48%"

并生成代码:

/********* Sample code generated by the curl command line tool **********
 * All curl_easy_setopt() options are documented at:
 * http://curl.haxx.se/libcurl/c/curl_easy_setopt.html
 ************************************************************************/
#include <curl/curl.h>

int
main (int argc, char *argv[])
{
  CURLcode ret;
  CURL *hnd;

  hnd = curl_easy_init ();
  curl_easy_setopt (hnd, CURLOPT_URL,
                    "http://download.finance.yahoo.com/d/quotes.csv?s=YHOO+GOOG+MSFT&f=sl1d1t1c1hgvbap2");
  curl_easy_setopt (hnd, CURLOPT_NOPROGRESS, 1L);
  curl_easy_setopt (hnd, CURLOPT_USERAGENT, "curl/7.35.0");
  curl_easy_setopt (hnd, CURLOPT_MAXREDIRS, 50L);
  curl_easy_setopt (hnd, CURLOPT_TCP_KEEPALIVE, 1L);

  /* Here is a list of options the curl code used that cannot get generated
     as source easily. You may select to either not use them or implement
     them yourself.

     CURLOPT_WRITEDATA set to a objectpointer
     CURLOPT_WRITEFUNCTION set to a functionpointer
     CURLOPT_READDATA set to a objectpointer
     CURLOPT_READFUNCTION set to a functionpointer
     CURLOPT_SEEKDATA set to a objectpointer
     CURLOPT_SEEKFUNCTION set to a functionpointer
     CURLOPT_ERRORBUFFER set to a objectpointer
     CURLOPT_STDERR set to a objectpointer
     CURLOPT_HEADERFUNCTION set to a functionpointer
     CURLOPT_HEADERDATA set to a objectpointer

   */

  ret = curl_easy_perform (hnd);

  curl_easy_cleanup (hnd);
  hnd = NULL;

  return (int) ret;
}

/**** End of sample code ****/

这篇关于如何像浏览器一样从 C 中的 URL 下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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