Recently I was working on a small project to provide a convevient way for my colleagues to use a service hosted on a server. In linux platform, we can use curl/wget. What about the windows platform?
There are two ways, one is to use MFC API, another is to use Internet Explorer API.
1. MFC API
CInternetSession hSession; CString szRand; szRand.Format(_T("&t=%d"),GetTickCount()); CString szUrl("http://xxx.com/~zhanghechuan/netprecompile?branch=" + szVersion + szRand); CStdioFile* pResp = hSession.OpenURL((LPCTSTR)szUrl); CString szResp; pResp->ReadString(szResp); // write this line to a file
Code above shows the correct way to do such thing. CInternetSession class provides OpenURL method, which take the target url as the first parameter and return the respond as CStdioFile*. Notice that, the url must start with http://, ftp:// etc. For more Detail, check MSDN .
Since we have the respond, a CStdioFile*, we can read lines from it, and write lines to a local file via another CStdioFile handler.
However, when using this approach, you may get an assertion failure when running, while compiling and linking proccesses are without error. To solve this problem, you need to do as follows:
- Include afxwin.h.
- Declare a global CWinApp class variable.
- Add code below in the body of your main function.
if(!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)){ return 1; }
2. IE API
An alternative is using IE API.
CString szArchive("http://xxx.com:8001/pm_templates/compile/52/compiled_template.tar.gz"); szRand.Format(_T("?t=%d"),GetTickCount()); szArchive += szRand; char* pComTplt = "compile/compiled_template.tar.gz"; URLDownloadToFile(NULL, (LPCTSTR)szArchive, pComTplt, 0, NULL);
Simple, isn't it? URLDownloadToFile takes the url as the second paramter and the local file which you wanna respond be written in as the third parameter(MSDN ). It's just like download files through a IE browser.
You may notice that, I add a timestamp for the url. That's because IE will cache the respond after first access it. Adding a timestamp forces IE to reaccess the url instead of reading from its cache.
However, there is a timeout bug in the afxinet package(http://support.microsoft.com/kb/q176420 ). When network condition is bad or the request service needs much time to fulfill, Using afxinet package could make your application crash. The alternative solution is using IE API which is heavier and less flexible.
相关推荐
rather, it's a book about writing 32-bit Windows applications in C++ using MFC rather than the Windows API as the chief means of accessing the operating system's essential features and services....
Enhance your Group Policy reach with the Group Policy Preferences, ADMX files, and additional add-ons Use every feature of the GPMC and become a top-notch administrator Troubleshoot Group Policy using...
* Identifies where specific techniques can--and cannot--be applied in the process * Includes inspiring and instructive case studies about the success of Six Sigma at Motorola, General Electric, ...
rather, it's a book about writing 32-bit Windows applications in C++ using MFC rather than the Windows API as the chief means of accessing the operating system's essential features and services....
This one–of–a–kind guide includes a step–by–step action plan for harnessing the power of the Internet to create compelling messages, get them in front of customers, and lead those customers into...
While the deployment of smart meters and advanced metering infrastructures in the USA seemed to reach a plateau when the rst edition of this book was published, the meter communication networks are ...
A comprehensive overview of the Internet of Things’ core concepts, technologies, and applications Internet of Things A to Z offers a holistic approach to the Internet of Things (IoT) model. The ...
It describes the most important state-of-the-art fundamentals and system details in the field, as well as many key aspects concerning the development and understanding of current and emergent ...
First,with the global coverage of Internet,we can reach out and touch anyone at any place of the world. Via Internet,we can send and receive E-mails at any time much faster, cheaper and more ...
Recruitment of hard-to-reach population subgroups via adaptations of the snowball sampling strategynhs_541 3
and robustness of these methods were evaluated via associated performance statistics, and the proposed hybrid approaches prove to be very suitable for solving the optimization problems of RSM-type. ...
Use Firewall to screen out hackers and malware that try to reach your computer over the internet. Use Keychain Access to manage your passwords for secure websites, FTP sites, network servers, ...
This book shows how to enhance an institution's presence on the Web with tools that integrate a variety of handy, popular programs. Application Programming Interfaces (APIs) are software tools that ...
C语言开发的 Across the great wall, we reach the freeland
The gears change in Chapter 2 as students reach the introduction of vector spaces. Throughout the book, the theory is motivated and reinforced by genuine applications, allowing pure mathematicians to...
Can Cloud Computing Reach The TOP500
Another advantage of FTPRush is the support of folder synchronization, so it can look after the identity of folders and contained files in upload and download directory. Identification of files that ...
A browser, an Internet connection, and some time to spend taming the JavaScript language are all that is needed to reach out into the vast reservoir of data on the Web. JavaScript and Open Data ...
23. The five layers in the Internet protocol stack are – from top to bottom – the application layer, the transport layer, the network layer, the link layer, and the physical layer. The principal ...
Finally, you will learn what you can do with Visual Studio 2017 to put mobile application development across multiple platforms within the reach of any developer. What you will learn Writing better...