读书人

怎么读取HttpSendRequest()获取的数据

发布时间: 2012-03-11 18:15:38 作者: rapoo

如何读取HttpSendRequest()获取的数据?
请问一下各位,我用HttpSendRequest()这个函数去请求数据,如果获取成功了,怎么知道获取成功了?
另外如果我请求的是一个字符串。我怎么读取这个字符串?

以我的程序举例。

C/C++ code
HINTERNET  hHttpOpenRequest = HttpOpenRequest( hInternetConn,         "GET",         "img/baidu_sylogo1.gif",         "HTTP/1.1",        "www.baidu.com",         0,         dwFlag,         0    );int bRet = HttpSendRequest( hHttpOpenRequest,         NULL,         0,         NULL,         0    );//这里返回值bRet为 TRUE , 应该是成功了。

我发送一个GET的request去获取baidu首页的图片。
我怎么知道我怎么去读取它呢。。。
如果是字符串呢???我怎么读取这个字符串???

谢谢各位!!



[解决办法]
3.发送httpSendRequest后,需要使用InternetReadFile来读取,重复多次读,直到返回数据为0byte为止
[解决办法]
// Open Internet session.
HINTERNET hSession = ::InternetOpen("MSDN SurfBear",
PRE_CONFIG_INTERNET_ACCESS,
NULL,
INTERNET_INVALID_PORT_NUMBER,
0) ;
// Connect to http://www.microsoft.com/.
HINTERNET hConnect = ::InternetConnect(hSession,
"http://www.microsoft.com/",
INTERNET_INVALID_PORT_NUMBER,
"",
"",
INTERNET_SERVICE_HTTP,
0,
0) ;
// Request the file /MSDN/MSDNINFO/ from the server.
HINTERNET hHttpFile = ::HttpOpenRequest(hConnect,
"GET",
"/MSDN/MSDNINFO/",
HTTP_VERSION,
NULL,
0,
INTERNET_FLAG_DONT_CACHE,
0) ;
// 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) ;
// Convert length from ASCII string to a DWORD.
DWORD dwFileSize = (DWORD)atol(bufQuery) ;
// Allocate a buffer for the file.
char* buffer = new char[dwFileSize+1] ;
// 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 ;
// Close all of the Internet handles.
::InternetCloseHandle(hHttpFile);
::InternetCloseHandle(hConnect) ;
::InternetCloseHandle(hSession) ;
// Display the file in an edit control.
pEditCtrl->SetWindowText(buffer) ;

读书人网 >VC/MFC

热点推荐