winnet api 实现Get请求
生活随笔
收集整理的這篇文章主要介紹了
winnet api 实现Get请求
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
mark一下!
#include <windows.h> #include <WinInet.h> #include <string>using namespace std;#pragma comment(lib,"wininet.lib")char* GetUrl2(const char* strName,char* strObject) {HINTERNET hSession = ::InternetOpen("MSDN SurfBear", PRE_CONFIG_INTERNET_ACCESS, NULL, INTERNET_INVALID_PORT_NUMBER, 0) ; // Connect to www.microsoft.com. HINTERNET hConnect = ::InternetConnect(hSession, strName, INTERNET_INVALID_PORT_NUMBER, "", "", INTERNET_SERVICE_HTTP, 0, 0) ; // Request the file /MSDN/MSDNINFO/ from the server. HINTERNET hHttpFile = ::HttpOpenRequest(hConnect, "GET", strObject,HTTP_VERSION, NULL, 0, INTERNET_FLAG_DONT_CACHE, 1); // Send the request. BOOL bSendRequest = ::HttpSendRequest(hHttpFile, NULL, 0, 0, 0); // Get the length of the file. char bufQuery[32] ; DWORD dwLengthBufQuery = sizeof(bufQuery); BOOL bQuery = ::HttpQueryInfo(hHttpFile, HTTP_QUERY_CONTENT_LENGTH,bufQuery,&dwLengthBufQuery,NULL) ; // Convert length from ASCII string to a DWORD. DWORD dwFileSize = (DWORD)atol(bufQuery) ; if (dwFileSize <= 2){return 0;}// Allocate a buffer for the file. char* buffer = new char[dwFileSize+1] ; ZeroMemory(buffer,0,sizeof(buffer));// Read the file into the buffer. DWORD dwBytesRead ; BOOL bRead = ::InternetReadFile(hHttpFile, buffer, dwFileSize+1, &dwBytesRead); // Put a zero on the end of the buffer. buffer[dwBytesRead] = 0 ;return buffer; }?
?
調用 GetUrl2("www.hao123.com","/task.asp?uid=123");
總結
以上是生活随笔為你收集整理的winnet api 实现Get请求的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VC菜单分割符
- 下一篇: PHP页面间参数传递的四种方式