Windows下比较简单的获取网页源码的方法

第一个方法是使用MFC里面的 <afxinet.h>

 CString GetHttpFileData(CString strUrl)
{
CInternetSession Session("Internet Explorer", );
CHttpFile *pHttpFile = NULL;
CString strData;
CString strClip;
pHttpFile = (CHttpFile*)Session.OpenURL(strUrl);
while ( pHttpFile->ReadString(strClip) )
{
strData += strClip;
}
return strData;
}

要讲一下,pHttpFile->ReadString() 每次可能只读一个数据片断,读多少次取决于网络状况,所以要把每次读到的数据加到总数据的尾部,用了CString 省去了缓冲区处理:) 
别忘了包含头文件#include <afxinet.h> 在工程设置,里面要选择 using MFC 要不然编译不了

第二种是使用WinNet的纯API实现的

#define MAXBLOCKSIZE 1024
#include <windows.h>
#include <wininet.h> #pragma comment(lib, "wininet.lib") void GetWebSrcCode(const char *Url); int _tmain(int argc, _TCHAR* argv[])
{
GetWebSrcCode("http://www.cnblogs.com/"); return ;
} void GetWebSrcCode(const char *Url)
{
HINTERNET hSession = InternetOpen("zwt", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, );
if (hSession != NULL)
{
HINTERNET hURL = InternetOpenUrl(hSession, Url, NULL, , INTERNET_FLAG_DONT_CACHE, );
if (hURL != NULL)
{
char Temp[MAXBLOCKSIZE] = {};
ULONG Number = ; FILE *stream;
if( (stream = fopen( "E:\\test.html", "wb" )) != NULL )
{
while (Number > )
{
InternetReadFile(hURL, Temp, MAXBLOCKSIZE - , &Number);
fwrite(Temp, sizeof (char), Number , stream);
}
fclose( stream );
} InternetCloseHandle(hURL);
hURL = NULL;
} InternetCloseHandle(hSession);
hSession = NULL;
}
}

第三种就是使用非封装过的Socket实现了

int main(int argc, char* argv[])
{
SOCKET hsocket;
SOCKADDR_IN saServer;
WSADATA wsadata;
LPHOSTENT lphostent;
int nRet;
char Dest[];
char* host_name="blog.sina.com.cn";
char* req="GET /s/blog_44acab2f01016gz3.html HTTP/1.1\r\n"
"User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET4.0C; .NET4.0E; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)\r\n"
"Host:blog.sina.com.cn\r\n\r\n"; // 初始化套接字
if(WSAStartup(MAKEWORD(,),&wsadata))
printf("初始化SOCKET出错!");
lphostent=gethostbyname(host_name);
if(lphostent==NULL)
printf("lphostent为空!");
hsocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
saServer.sin_family = AF_INET;
saServer.sin_port = htons();
saServer.sin_addr =*((LPIN_ADDR)*lphostent->h_addr_list);
// 利用SOCKET连接
nRet = connect(hsocket,(LPSOCKADDR)&saServer,sizeof(SOCKADDR_IN));
if(nRet == SOCKET_ERROR)
{
printf("建立连接时出错!");
closesocket(hsocket);
return ;
}
// 利用SOCKET发送 nRet = send(hsocket,req,strlen(req),);
if(nRet==SOCKET_ERROR)
{
printf("发送数据包时出错!");
closesocket(hsocket);
}
nRet=;
while(nRet>)
{
// 接收返回数据包
nRet=recv(hsocket,(LPSTR)Dest,sizeof(Dest),);
if(nRet>)
Dest[nRet]=;
else
Dest[]=;
char sDest[] = {};
UTF8_2_GB2312(sDest,nRet,Dest,nRet);
// 显示返回数据包的大小、内容
//printf("\nReceived bytes:%d\n",nRet);
printf("Result:\n%s",sDest);
}
}

另外,以上我们获取网页的时候,获取到的可能是UTF8,似乎目前大多数网站都用的这种编码吧!下面是编码转换。

void UTF_8ToUnicode(wchar_t* pOut,char *pText)
{
char* uchar = (char *)pOut;
uchar[] = ((pText[] & 0x0F) << ) + ((pText[] >> ) & 0x0F);
uchar[] = ((pText[] & 0x03) << ) + (pText[] & 0x3F);
}
void Gb2312ToUnicode(wchar_t* pOut,char *gbBuffer)
{
::MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,gbBuffer,,pOut,);
}
void UTF_8ToGB2312(char*pOut, char *pText, int pLen)
{
char Ctemp[];
memset(Ctemp,,);
int i = ,j = ;
while(i < pLen)
{
if(pText[i] >= )
{
pOut[j++] = pText[i++];
}
else
{
WCHAR Wtemp;
UTF_8ToUnicode(&Wtemp,pText + i);
UnicodeToGB2312(Ctemp,Wtemp);
pOut[j] = Ctemp[];
pOut[j + ] = Ctemp[];
i += ;
j += ;
}
}
pOut[j] ='\n';
return;
}

这是是转换成GB2312的代码

上一篇:centos 安装 maven


下一篇:抖音人脸识别Autojs脚本