在 VC 中用 WinInet 的 CInternetSession::OpenURL(url),得到一个 CFile,读取其中的内容即可,详细代码如下
- #include <stdio.h>
- #include <afxinet.h>
-
- int main(int argc, char* argv[])
- {
- CInternetSession session("HttpClient");
- char * url = " http://www.imobile.com.cn/simcard.php?simcard=1392658";
- CHttpFile* pfile = (CHttpFile *)session.OpenURL(url);
-
- DWORD dwStatusCode;
- pfile -> QueryInfoStatusCode(dwStatusCode);
- if(dwStatusCode == HTTP_STATUS_OK)
- {
- CString content;
- CString data;
- while (pfile -> ReadString(data))
- {
- content += data + "\r\n";
- }
- content.TrimRight();
- printf(" %s\n " ,(LPCTSTR)content);
- }
- pfile -> Close();
- delete pfile;
- session.Close();
-
- return 0 ;
- }
其他如不从缓存中读取内容及如何使用代理连接现在就不说了,可以参考下面的链接,或者下次补上。另外不妨看看 Java 是如何读取 URL 内容的,更简单
- GetMethod httpMethod = new GetMethod("http://unmi.blogcn.com");
- int statusCode = new HttpClient().executeMethod(httpMethod);
- if(statusCode == HttpStatus.SC_OK)
- {
- System.out.println(httpMethod.getResponseBodyAsString());
- }
- httpMethod.releaseConnection();
posted on 2008-12-22 11:12
高山流水 阅读(534)
评论(0) 编辑 收藏 引用