#include #include #include #pragma comment(lib, "ws2_32.lib")void geturl(char *url)
; socket sockfd;
struct sockaddr_in addr;
struct hostent *purl;
char myurl[bufsiz];
char *phost = 0, *pget = 0;
char host[bufsiz], get[bufsiz];
char header[bufsiz] = "";
static char text[bufsiz];
int i;
/* * windows下使用socket必須用wsastartup初始化,否則不能呼叫
*/ if(wsastartup(makeword(2,2), &wsadata))
/** 分離url中的主機位址和相對路徑
*/ strcpy(myurl, url);
for (phost = myurl; *phost != '/' && *phost != '\0'; ++phost);
if ( (int)(phost - myurl) == strlen(myurl) )
strcpy(get, "/");
else
strcpy(get, phost);
*phost = '\0';
strcpy(host, myurl);
printf("%s\n%s\n", host, get);
/* * 設定socket引數,並未真正初始化
*/ sockfd = socket(pf_inet, sock_stream, ipproto_tcp);
purl = gethostbyname(host);
addr.sin_family = af_inet;
addr.sin_addr.s_addr = *((unsigned long*)purl->h_addr);
addr.sin_port = htons(80);
/* * 組織傳送到web伺服器的資訊
* 為何要傳送下面的資訊請參考http協議的約定
/* * 連線到伺服器,傳送請求header,並接受反饋(即網頁源**)
*/ connect(sockfd,(sockaddr *)&addr,sizeof(addr));
send(sockfd, header, strlen(header), 0);
while ( recv(sockfd, text, bufsiz, 0) > 0)
closesocket(sockfd);
wsacleanup();
}int main()
利用MFC獲取網頁內容
include include include include include include include include using namespace std string gethtml cstring url pfile close delete pfile session.close ...
使用SOCKET獲取網頁的內容
使用fsockopen 函式來實現獲取頁面資訊,完整 如下 設定字符集 由於要抓取的網易 字符集編碼是gbk編碼 header content type text html charset gb2312 設定中國時區 date default timezone set prc 頁面網域名稱 host...
c 獲取網頁內容
http s34.pet.mop.com petwin.html title 檢視 nclick p.showpetinfo 0,機警小雞 49597282678812,false,false,false,true p.setfuji 0 return false 機警小雞 r nhttp s34....