三種獲得網頁源碼的辦法(應用MFC/Socket完成)。本站提示廣大學習愛好者:(三種獲得網頁源碼的辦法(應用MFC/Socket完成))文章只能為提供參考,不一定能成為您想要的結果。以下是三種獲得網頁源碼的辦法(應用MFC/Socket完成)正文
第一個辦法是應用MFC外面的
<afxinet.h>
CString GetHttpFileData(CString strUrl)
{
CInternetSession Session("Internet Explorer", 0);
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.jb51.net/");
return 0;
}
void GetWebSrcCode(const char *Url)
{
HINTERNET hSession = InternetOpen("zwt", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hSession != NULL)
{
HINTERNET hURL = InternetOpenUrl(hSession, Url, NULL, 0, INTERNET_FLAG_DONT_CACHE, 0);
if (hURL != NULL)
{
char Temp[MAXBLOCKSIZE] = {0};
ULONG Number = 1;
FILE *stream;
if( (stream = fopen( "E:\\test.html", "wb" )) != NULL )
{
while (Number > 0)
{
InternetReadFile(hURL, Temp, MAXBLOCKSIZE - 1, &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[3000];
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(2,2),&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(80);
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 0;
}
// 應用SOCKET發送
nRet = send(hsocket,req,strlen(req),0);
if(nRet==SOCKET_ERROR)
{
printf("發送數據包時失足!");
closesocket(hsocket);
}
nRet=1;
while(nRet>0)
{
// 吸收前往數據包
nRet=recv(hsocket,(LPSTR)Dest,sizeof(Dest),0);
if(nRet>0)
Dest[nRet]=0;
else
Dest[0]=0;
char sDest[3000] = {0};
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[1] = ((pText[0] & 0x0F) << 4) + ((pText[1] >> 2) & 0x0F);
uchar[0] = ((pText[1] & 0x03) << 6) + (pText[2] & 0x3F);
}
void Gb2312ToUnicode(wchar_t* pOut,char *gbBuffer)
{
::MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,gbBuffer,2,pOut,1);
}
void UTF_8ToGB2312(char*pOut, char *pText, int pLen)
{
char Ctemp[4];
memset(Ctemp,0,4);
int i =0 ,j = 0;
while(i < pLen)
{
if(pText[i] >= 0)
{
pOut[j++] = pText[i++];
}
else
{
WCHAR Wtemp;
UTF_8ToUnicode(&Wtemp,pText + i);
UnicodeToGB2312(Ctemp,Wtemp);
pOut[j] = Ctemp[0];
pOut[j + 1] = Ctemp[1];
i += 3;
j += 2;
}
}
pOut[j] ='\n';
return;
}
這是是轉換成GB2312的代碼