三種獲取網(wǎng)頁源碼的方法(使用MFC/Socket實(shí)現(xiàn))
第一個方法是使用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() 每次可能只讀一個數(shù)據(jù)片斷,讀多少次取決于網(wǎng)絡(luò)狀況,所以要把每次讀到的數(shù)據(jù)加到總數(shù)據(jù)的尾部,用了CString 省去了緩沖區(qū)處理:)
別忘了包含頭文件#include <afxinet.h> 在工程設(shè)置,里面要選擇 using MFC 要不然編譯不了
第二種是使用WinNet的純API實(shí)現(xiàn)的
#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實(shí)現(xiàn)了
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發(fā)送
nRet = send(hsocket,req,strlen(req),0);
if(nRet==SOCKET_ERROR)
{
printf("發(fā)送數(shù)據(jù)包時出錯!");
closesocket(hsocket);
}
nRet=1;
while(nRet>0)
{
// 接收返回?cái)?shù)據(jù)包
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);
// 顯示返回?cái)?shù)據(jù)包的大小、內(nèi)容
//printf("\nReceived bytes:%d\n",nRet);
printf("Result:\n%s",sDest);
}
}
另外,以上我們獲取網(wǎng)頁的時候,獲取到的可能是UTF8,似乎目前大多數(shù)網(wǎng)站都用的這種編碼吧!下面是編碼轉(zhuǎn)換。
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;
}
這是是轉(zhuǎn)換成GB2312的代碼
欄 目:C語言
下一篇:solaris操作系統(tǒng)做c應(yīng)用程序開發(fā)步驟
本文標(biāo)題:三種獲取網(wǎng)頁源碼的方法(使用MFC/Socket實(shí)現(xiàn))
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/3833.html
您可能感興趣的文章
- 01-10Linux C 獲取進(jìn)程退出值的實(shí)現(xiàn)代碼
- 01-10解析Linux下的時間函數(shù):設(shè)置以及獲取時間的方法
- 01-10DHCP:解析開發(fā)板上動態(tài)獲取ip的2種實(shí)現(xiàn)方法詳解
- 01-10基于linux下獲取時間函數(shù)的詳解
- 01-10linux c 獲取本機(jī)公網(wǎng)IP的實(shí)現(xiàn)方法
- 01-10用c 獲取文件MD5值的實(shí)現(xiàn)方法
- 01-10ubuntu中打開終端的三種解決方法
- 01-10使用C語言中的time函數(shù)獲取系統(tǒng)時間
- 01-10linux c程序中獲取shell腳本輸出的實(shí)現(xiàn)方法
- 01-10c++中new的三種用法詳細(xì)解析


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery