VC中怎样使用CInternetSession抓取网页内容
来源:优易学  2011-12-31 15:05:48   【优易学:中国教育考试门户网】   资料下载   IT书店
  在 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 + "rn";
  }
  content.TrimRight();
  printf(" %sn " ,(LPCTSTR)content);
  }
  pfile -> Close();
  delete pfile;
  session.Close();
  return  0 ;
  }
  #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 + "rn";
  }
  content.TrimRight();
  printf(" %sn " ,(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();
  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();
  内容取过来之后,总是希望从中拣出需要的数据,可惜 VC6 中没有自己的正则表达式库,所以下一步要学用 boost 的正则表达式库。

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训