Wininet 怎么获取网页返回的信息保存到string
Wininet 如何获取网页返回的信息保存到string?
发送一个数据包,如何保存返回的红色那部分信息,具体是哪个函数额
返回
------解决方案--------------------
CHttpFile* pFile = CInternetSession::OpenURL();
然后调用
CHttpFile::QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF,...)
简单例子代码:
发送一个数据包,如何保存返回的红色那部分信息,具体是哪个函数额
- HTML code
GET /files?aid=1&cid=0&o=&asc=0&offset=0&show_dir=1&limit=54&source=&format=json&_t=1326943744867 HTTP/1.1 Host: **** Connection: keep-alive User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.75 Safari/535.7 Content-Type: application/x-www-form-urlencoded Accept: */* Referer: **** Accept-Encoding: gzip,deflate,sdch Accept-Language: zh-CN,zh;q=0.8 Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3
返回
- C/C++ code
HTTP/1.1 200 OK Server: nginx Date: Thu, 19 Jan 2012 03:31:12 GMT Content-Type: text/html Connection: keep-alive X-HT: 0.0014979839324951 Content-Length: 825 X-Data-Source: Index X-Take-Time: 0.081 Vary: Accept-Encoding Powered-By-YlmF: CHN-GD-DG-TEL-4 {"count":2,"data":[{"fid":26070093,"aid":"1","cid":"0","n":"\u3010\u90a3\u4e9b\u5e74\uff0c\u6211\u4eec\u4e00\u8d77\u8ffd\u7684\u5973\u5b69\u3011\u3010\u9ad8\u6e05\u7248DVD-RMVB.\u56fd\u8bed\u4e2d\u5b57\u3011\u30102011\u6700\u65b0\u53f0\u6e7e\u7968\u623f\u559c\u5267\u7231\u60c5\u5927\u7247\u3011.torrent","s":"42818","sta":"1","pt":"0","pc":"bhgsm112","p":"0","m":"0","t":"2012-01-09","d":1,"c":"1","sh":0,"e":0,"ico":"torrent"},{"fid":25905999,"aid":"1","cid":"0","n":"QQ2011Beta3.exe","s":"44070264","sta":"1","pt":"0","pc":"cl1ate7c","p":"0","m":"0","t":"2011-12-13","d":1,"c":"0","sh":0,"e":0,"ico":"exe"}],"data_source":"Index","time":"0.069","offset":0,"page_size":54,"aid":1,"cid":0,"is_asc":0,"path":[{"name":"\u6211\u7684\u6587\u4ef6","aid":1,"cid":0,"pid":0}],"order":"user_ptime","state":true,"error":"","errNo":0}
------解决方案--------------------
CHttpFile* pFile = CInternetSession::OpenURL();
然后调用
CHttpFile::QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF,...)
简单例子代码:
- C/C++ code
try { CInternetSession session(_T("Session")); CHttpFile* pFile = (CHttpFile*)session.OpenURL(_T("http://www.google.com")); if(pFile) { CString str(_T("")); pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, str); AfxMessageBox(str); pFile->Close(); delete pFile; pFile = NULL; } session.Close(); } catch (CException* e) { e->ReportError(); e->Delete(); }
------解决方案--------------------
我用的是window api 没有MFC的封装类,贴一个获取(请求或者响应)头信息函数,一般在请求结束,获取正文前使用——
- C/C++ code
CString GetHead(HINTERNET hRequest, BOOL requestOrResponse) { DWORD dwInfoLevel; if(requestOrResponse) dwInfoLevel = HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS; else dwInfoLevel = HTTP_QUERY_RAW_HEADERS_CRLF; DWORD headLength; ::HttpQueryInfo(hRequest, dwInfoLevel ,NULL,&headLength,NULL); char* headData = new char[headLength+1]; ::HttpQueryInfo(hRequest, dwInfoLevel ,headData,&headLength,NULL); headData[headLength] = '\0'; CString strCookie = headData; if(headData) { delete headData; headData = NULL; } return strCookie; }