如何读取HttpSendRequest()获取的数据?

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

以我的程序举例。

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首页的图片。
我怎么知道我怎么去读取它呢。。。
如果是字符串呢???我怎么读取这个字符串???

谢谢各位!!

...全文
240 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
oyljerry 2011-12-04
  • 打赏
  • 举报
回复
// 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) ;
oyljerry 2011-12-04
  • 打赏
  • 举报
回复
3.发送httpSendRequest后,需要使用InternetReadFile来读取,重复多次读,直到返回数据为0byte为止

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧