Alright I've spent a good three days trying this, here's the scenario:
好吧,我花了三天时间尝试这个,这是场景:
I want to download a '.csv' file from Google and then do stuff with the data from the file. It's for a Win32 Console Application. I have the latter down, I just cannot for the life of me figure out how to download the file. I've heard of libcurl, curlpp, ptypes, rolling my own, just using the .NET api, and been told a bunch of times:
我想从谷歌下载'.csv'文件,然后从文件中获取数据。它适用于Win32控制台应用程序。我有后者,我只是不能为我的生活弄清楚如何下载文件。我听说过libcurl,curlpp,ptypes,我自己滚动,只是使用.NET api,并被告知了很多次:
...it's just a GET request
......这只是一个GET请求
Well that's all well and good, but I must be missing something because it seems like everyone was just born knowing how to do this. I have been combing through books looking to figure this out and even had a huge problem with LNKerrors after traveling down the road with "The Art of C++" for a while.
嗯,这一切都很好,但我必须错过一些东西,因为似乎每个人都知道如何做到这一点。我一直在梳理书籍,想要弄清楚这一点,甚至在使用“C ++的艺术”一段时间前往路上后,LNKerrors也遇到了一个巨大的问题。
All that being said, I have learned a LOT from this, but at this point I just want to know how to do it. The API for C++ is seriously lacking, no example code to be found. Tutorials online are almost non-existent. And no book out there seems to think this is important.
总而言之,我从中学到了很多东西,但此时我只想知道如何去做。严重缺乏C ++ API,没有找到示例代码。在线教程几乎不存在。没有书似乎认为这很重要。
Can someone please throw me a life raft? I'm a man on the edge here.
有人可以请我救生筏吗?我是一个处于边缘的人。
edit
By "from Google" I mean that I want to download a .csv file that they host. An example can be found here.
“来自Google”我的意思是我想下载他们托管的.csv文件。这里有一个例子。
8 个解决方案
#1
You should be able to bend this to your will.
你应该能够根据自己的意愿弯曲它。
Now that I have kinda answered your question. Why C++? Nothing against the language, but pick the best language for the job. Perl, PHP, and Python(and I am sure more) all have great documentation and support on this kind of operation.
现在我已经回答了你的问题。为何选择C ++?没有什么可以反对语言,但选择最好的语言来完成这项工作。 Perl,PHP和Python(我相信更多)都有很好的文档和支持这种操作。
In perl(the one I am familiar with) it's just about 3-5 lines of code.
在perl(我熟悉的那个)中,它只有大约3-5行代码。
#3
Since you're on the Win32 platform, there is one built in libary you can use to implement a GET request in a relatively straightforward way: WinInet, which is part of the Win32 SDK. The basic reference for WinInet can be found on MSDN.
由于您使用的是Win32平台,因此可以使用一个内置的库来以相对简单的方式实现GET请求:WinInet,它是Win32 SDK的一部分。 WinInet的基本参考可以在MSDN上找到。
Be warned there will be some rough sledding ahead if you're not familiar with the Win32 API. There is a fairly useful block of example code here
请注意,如果您不熟悉Win32 API,将会有一些粗略的雪橇。这里有一个相当有用的示例代码块
You will get linker errors if you fail to add the appropriate library reference to your project. It sounds like you've learned some lessons there already so I'll keep it breif, but be assured that you will find references to both the libraries and header file references you will need in the Win32 documentation (you just have to learn where on the page to look for it).
如果未能将适当的库引用添加到项目中,则会出现链接器错误。听起来你已经在那里学到了一些课程所以我会保持它的简洁,但请放心,你会在Win32文档中找到你需要的库和头文件引用的引用(你只需要知道在哪里上寻找它的页面)。
#4
You can't do that with the standard library. Since it's win32, you can follow Paul Keister's advice.
你不能用标准库做到这一点。既然它是win32,你可以按照Paul Keister的建议。
But have a look at libcurl, and since you are in C++, I'd recommend POCO it's very usefull too. They are very handy tools!
但是看看libcurl,既然你是C ++,我推荐POCO它也非常有用。它们是非常方便的工具!
#6
> no example code to be found
>没有找到示例代码
???
Dozens of samples have been posted for 15 years.. See on api ng news://comp.os.ms-windows.programmer.win32 (samples in C)
数十个样本已经发布了15年。请参阅api ng news://comp.os.ms-windows.programmer.win32(C中的样本)
(COM, INET, etc)
(COM,INET等)
#7
I too have been looking for a good http lib for C++ but haven't found the 'perfect' lib. Boost.Asio is fantastic but quite low level for a simple http library (though the http client examples are a good start). The C++ Networking Library* (cpp-netlib) looks like it's on track, building a higher-level api on top of asio, but is not very mature. Check out the http_client example.
我也一直在为C ++寻找一个好的http lib,但还没有找到'完美'的lib。 Boost.Asio非常棒但是对于一个简单的http库来说水平相当低(尽管http客户端示例是一个很好的开始)。 C ++ Networking Library *(cpp-netlib)看起来像是在轨道上,在asio之上构建一个更高级别的api,但还不是很成熟。查看http_client示例。
I'm either going to write my own wrappers around asio or extend (and contribute) to cpp-netlib unless I can find a better option.
我要么围绕asio编写我自己的包装器,要么扩展(并贡献)cpp-netlib,除非我能找到更好的选择。
(BTW libcurl is not a better option for me. It's a much larger api than I want and very unwieldy for something like an http client, especially in C++. Don't get me wrong, it's a great library but not appropriate for a simple http client.)
(BTW libcurl对我来说不是一个更好的选择。它是一个比我想要的更大的api,对于像http客户端这样的东西非常笨重,特别是在C ++中。不要误解我的意思,它是一个很棒的库,但不适合简单的http客户端。)
#8
C++ doesn't really have an API per se. To do a GET request, your program needs to open a TCP socket connection to the site in question, and send the information down the socket to conform with the HTTP specification, see here. There are likely a number of libraries you could find to help with this, for example, you could look at what the open source program wget uses. If you're coding it yourself, it'd be helpful to use a packet sniffing tool. You could see exactly what your browser sends to the web server to get the file back, and just send exactly that message. It's not too hard, but it is pretty hard. TCP is a pretty big learning curve initially.
C ++本身并没有真正的API。要执行GET请求,您的程序需要打开与相关站点的TCP套接字连接,并将信息发送到套接字以符合HTTP规范,请参见此处。您可以找到许多库来帮助解决这个问题,例如,您可以查看开源程序所使用的内容。如果您自己编写代码,使用数据包嗅探工具会很有帮助。您可以准确地看到浏览器发送到Web服务器以获取文件的内容,并且只发送该消息。这不是太难,但很难。 TCP最初是一个相当大的学习曲线。
By the way, what do you mean by "from google?"
那么,你是什么意思“来自谷歌?”
#1
You should be able to bend this to your will.
你应该能够根据自己的意愿弯曲它。
Now that I have kinda answered your question. Why C++? Nothing against the language, but pick the best language for the job. Perl, PHP, and Python(and I am sure more) all have great documentation and support on this kind of operation.
现在我已经回答了你的问题。为何选择C ++?没有什么可以反对语言,但选择最好的语言来完成这项工作。 Perl,PHP和Python(我相信更多)都有很好的文档和支持这种操作。
In perl(the one I am familiar with) it's just about 3-5 lines of code.
在perl(我熟悉的那个)中,它只有大约3-5行代码。
#2
#3
Since you're on the Win32 platform, there is one built in libary you can use to implement a GET request in a relatively straightforward way: WinInet, which is part of the Win32 SDK. The basic reference for WinInet can be found on MSDN.
由于您使用的是Win32平台,因此可以使用一个内置的库来以相对简单的方式实现GET请求:WinInet,它是Win32 SDK的一部分。 WinInet的基本参考可以在MSDN上找到。
Be warned there will be some rough sledding ahead if you're not familiar with the Win32 API. There is a fairly useful block of example code here
请注意,如果您不熟悉Win32 API,将会有一些粗略的雪橇。这里有一个相当有用的示例代码块
You will get linker errors if you fail to add the appropriate library reference to your project. It sounds like you've learned some lessons there already so I'll keep it breif, but be assured that you will find references to both the libraries and header file references you will need in the Win32 documentation (you just have to learn where on the page to look for it).
如果未能将适当的库引用添加到项目中,则会出现链接器错误。听起来你已经在那里学到了一些课程所以我会保持它的简洁,但请放心,你会在Win32文档中找到你需要的库和头文件引用的引用(你只需要知道在哪里上寻找它的页面)。
#4
You can't do that with the standard library. Since it's win32, you can follow Paul Keister's advice.
你不能用标准库做到这一点。既然它是win32,你可以按照Paul Keister的建议。
But have a look at libcurl, and since you are in C++, I'd recommend POCO it's very usefull too. They are very handy tools!
但是看看libcurl,既然你是C ++,我推荐POCO它也非常有用。它们是非常方便的工具!
#5
Another option is WinHTTP, which also comes with Windows.
另一个选项是WinHTTP,它也随Windows一起提供。
#6
> no example code to be found
>没有找到示例代码
???
Dozens of samples have been posted for 15 years.. See on api ng news://comp.os.ms-windows.programmer.win32 (samples in C)
数十个样本已经发布了15年。请参阅api ng news://comp.os.ms-windows.programmer.win32(C中的样本)
(COM, INET, etc)
(COM,INET等)
#7
I too have been looking for a good http lib for C++ but haven't found the 'perfect' lib. Boost.Asio is fantastic but quite low level for a simple http library (though the http client examples are a good start). The C++ Networking Library* (cpp-netlib) looks like it's on track, building a higher-level api on top of asio, but is not very mature. Check out the http_client example.
我也一直在为C ++寻找一个好的http lib,但还没有找到'完美'的lib。 Boost.Asio非常棒但是对于一个简单的http库来说水平相当低(尽管http客户端示例是一个很好的开始)。 C ++ Networking Library *(cpp-netlib)看起来像是在轨道上,在asio之上构建一个更高级别的api,但还不是很成熟。查看http_client示例。
I'm either going to write my own wrappers around asio or extend (and contribute) to cpp-netlib unless I can find a better option.
我要么围绕asio编写我自己的包装器,要么扩展(并贡献)cpp-netlib,除非我能找到更好的选择。
(BTW libcurl is not a better option for me. It's a much larger api than I want and very unwieldy for something like an http client, especially in C++. Don't get me wrong, it's a great library but not appropriate for a simple http client.)
(BTW libcurl对我来说不是一个更好的选择。它是一个比我想要的更大的api,对于像http客户端这样的东西非常笨重,特别是在C ++中。不要误解我的意思,它是一个很棒的库,但不适合简单的http客户端。)
#8
C++ doesn't really have an API per se. To do a GET request, your program needs to open a TCP socket connection to the site in question, and send the information down the socket to conform with the HTTP specification, see here. There are likely a number of libraries you could find to help with this, for example, you could look at what the open source program wget uses. If you're coding it yourself, it'd be helpful to use a packet sniffing tool. You could see exactly what your browser sends to the web server to get the file back, and just send exactly that message. It's not too hard, but it is pretty hard. TCP is a pretty big learning curve initially.
C ++本身并没有真正的API。要执行GET请求,您的程序需要打开与相关站点的TCP套接字连接,并将信息发送到套接字以符合HTTP规范,请参见此处。您可以找到许多库来帮助解决这个问题,例如,您可以查看开源程序所使用的内容。如果您自己编写代码,使用数据包嗅探工具会很有帮助。您可以准确地看到浏览器发送到Web服务器以获取文件的内容,并且只发送该消息。这不是太难,但很难。 TCP最初是一个相当大的学习曲线。
By the way, what do you mean by "from google?"
那么,你是什么意思“来自谷歌?”