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....
- **`javax.servlet` Package**: This package contains the classes and interfaces that define the Servlet API, including the `Servlet`, `ServletConfig`, and `ServletContext` interfaces. - **Servlet ...
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...
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...
To effectively track the usage of multimedia resources on the system platform, an automated post-statistics analysis module is implemented to statistic the collection and download status of video files....
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 ...
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, ...
C语言开发的 Across the great wall, we reach the freeland
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 ...
For inquiries or business dealings with Jacobs Joinery and Shopfitters cc, you can reach them through the following contact information: - **Physical Address:** 13 Bulman Road, Mkondeni, ...
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...
Create responsive applications that make snappy connections for mobile browsers and give your website the latest design and development advantages to reach mobile devices. At the end of this course, ...
It’s the properties and values that we focus on in this book, and the patterns and prac‐ tices we see companies employ in order to reach their unique goals. If you’re looking for a way to identify ...