从Cpp中的URL下载时获取空CSV [英] Getting an empty CSV when downloading from URL in Cpp
问题描述
我的目标是使用Cpp从特定的URL获取CSV或XLS。
打开以下链接时
http://www.centrodeinformacao.ren.pt/userControls/GetExcel.aspx?T=CRG&P=01-01-2007&variation=PT
,可以在浏览器工具中查看
302重定向,并且实际上是从以下URL下载文件
http://www.centrodeinformacao.ren.pt/_layouts/CI .GetExcel / SafeGetExcel.aspx?T = CRG& P = 02-01-2007& variation = PT
下一张图片(请求URL)
如果我转到手动两个链接,.xls文件下载就很好了o我们最好还是使用重定向后的那个。
我决定继续使用
- 打开命令提示符,cd C:/ Program Files / vcpkg,运行
bootstrap-vcpkg.bat
和运行后 vcpkg集成安装
2。安装libcurl
- 运行
vcpkg安装curl
3。创建一个新项目
- 只需创建Visual C ++> Windows桌面> Windows控制台应用程序
可以使用 #include< curl / curl.h>
立即
4。当前结果
然后,从以下答案中得到启发
- 解决方案
一次到该特定URL,将下载.xls文件。我不介意使用XLS而不是CSV,因此将其更改为我可以按预期方式获取文件。
downloadFile( ; http://www.centrodeinformacao.ren.pt/_layouts/CI.GetExcel/SafeGetExcel.aspx?T = CRG& P = 01-01-2007&variation = PT, C:\\Users\ molecoder桌面 test.xls);
这是最终代码
#include pch.h
#定义_CRT_SECURE_NO_WARNINGS
#include< iostream>
#include< iostream>
#include< stdio.h>
#include< curl / curl.h>
#include< string.h>
size_t write_data(void * ptr,size_t size,size_t nmemb,FILE * stream){
size_t写= fwrite(ptr,size,nmemb,流);
书面回报;
}
无效downloadFile(const char * url,const char * fname){
CURL * curl;
文件* fp;
CURLcode res;
curl = curl_easy_init();
if(curl){
fp = fopen(fname, wb);
curl_easy_setopt(curl,CURLOPT_URL,url);
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,write_data);
curl_easy_setopt(curl,CURLOPT_WRITEDATA,fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
}
}
int main(void){
downloadFile( http://www.centrodeinformacao.ren.pt/_layouts/ CI.GetExcel / SafeGetExcel.aspx?T = CRG& P = 01-01-2007& variation = PT, C:\\Users\\molecoder\\Desktop\\test。 xls);
}
My goal is to get a CSV or XLS from a specific URL using Cpp.
When opening the following link
http://www.centrodeinformacao.ren.pt/userControls/GetExcel.aspx?T=CRG&P=01-01-2007&variation=PT
, can see in Browser tools
a 302 redirect and the file being actually downloaded from the following URL
http://www.centrodeinformacao.ren.pt/_layouts/CI.GetExcel/SafeGetExcel.aspx?T=CRG&P=02-01-2007&variation=PT
as shown in the next image (Request URL)
If I go to any of the two links manually, the a .xls file downloads just fine so we might as well use the one after redirection.
I've decided to move on an use libcurl with Visual Studio 2017 on my W10 machine. The recommended way to include libcurl in a Visual Studio 2017 project is to use vcpkg and that's what I used.
1. Install vcpkg
- Open Git Bash, cd C:/Program Files/ and cloned this repo.
- Open Command Prompt, cd C:/Program Files/vcpkg, run
bootstrap-vcpkg.bat
and after run
vcpkg integrate install
2. Install libcurl
- Run
vcpkg install curl
3. Create a new project
- Simply create Visual C++ > Windows Desktop > Windows Console Application
to be able to use
#include <curl/curl.h>
right away
4. Current Result
Then, inspired in the following answers
And using the following code
#include "pch.h" #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <iostream> #include <stdio.h> #include <curl/curl.h> #include <string.h> size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) { size_t written = fwrite(ptr, size, nmemb, stream); return written; } void downloadFile(const char* url, const char* fname) { CURL *curl; FILE *fp; CURLcode res; curl = curl_easy_init(); if (curl) { fp = fopen(fname, "wb"); curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); res = curl_easy_perform(curl); curl_easy_cleanup(curl); fclose(fp); } } int main(void) { downloadFile("http://www.centrodeinformacao.ren.pt/_layouts/CI.GetExcel/SafeGetExcel.aspx?T=CRG&P=01-01-2007&variation=PT", "C:\\Users\\molecoder\\Desktop\\test.csv"); }
I can see a test.csv in the desired folder but it's an empty file.
解决方案Once going to that specific URL, a .xls file downloads. I don't mind getting a XLS instead of a CSV and so changing it to I was able to get the file as expected.
downloadFile("http://www.centrodeinformacao.ren.pt/_layouts/CI.GetExcel/SafeGetExcel.aspx?T=CRG&P=01-01-2007&variation=PT", "C:\\Users\\molecoder\\Desktop\\test.xls");
This is the final code
#include "pch.h" #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <iostream> #include <stdio.h> #include <curl/curl.h> #include <string.h> size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) { size_t written = fwrite(ptr, size, nmemb, stream); return written; } void downloadFile(const char* url, const char* fname) { CURL *curl; FILE *fp; CURLcode res; curl = curl_easy_init(); if (curl) { fp = fopen(fname, "wb"); curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); res = curl_easy_perform(curl); curl_easy_cleanup(curl); fclose(fp); } } int main(void) { downloadFile("http://www.centrodeinformacao.ren.pt/_layouts/CI.GetExcel/SafeGetExcel.aspx?T=CRG&P=01-01-2007&variation=PT", "C:\\Users\\molecoder\\Desktop\\test.xls"); }
这篇关于从Cpp中的URL下载时获取空CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!