方便易用,传入URL,返回对应页面的内容
#include <iostream>
#include <string>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
void parseHostAndPagePath(const string url, string &hostUrl, string &pagePath){
hostUrl=url;
pagePath="/";
int pos=hostUrl.find("https://");
if(-1!=pos)
hostUrl=hostUrl.replace(pos,7,"");
pos=hostUrl.find("https://");
if(-1!=pos)
hostUrl=hostUrl.replace(pos,8,"");
pos=hostUrl.find("/");
if(-1!=pos){
pagePath=hostUrl.substr(pos);
hostUrl=hostUrl.substr(0,pos);
}
}
string getPageContent(const string url){
struct hostent *host;
string hostUrl, pagePath;
parseHostAndPagePath(url, hostUrl, pagePath);
if(0==(host=gethostbyname(hostUrl.c_str()))){
cout<<"gethostbyname error\n"<<endl;
exit(1);
}
struct sockaddr_in pin;
in