在 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 的正則表達式庫。