当前位置: 首页>>代码示例>>C++>>正文


C++ HttpHeader::getHeaderStr方法代码示例

本文整理汇总了C++中HttpHeader::getHeaderStr方法的典型用法代码示例。如果您正苦于以下问题:C++ HttpHeader::getHeaderStr方法的具体用法?C++ HttpHeader::getHeaderStr怎么用?C++ HttpHeader::getHeaderStr使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在HttpHeader的用法示例。


在下文中一共展示了HttpHeader::getHeaderStr方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: testGetWebPage

void testGetWebPage(Url&url) {

    HttpClient client;
    HttpHeader httpheader;
    HttpContent httpcontent;
    client.requestWebPage(url, httpheader, httpcontent);
    //string header;
    //client.requestHeader(url,header);
    //cout<<header<<endl;
    cout << httpheader.getHeaderStr() << endl;
}
开发者ID:skiloop,项目名称:wmtor,代码行数:11,代码来源:test.cpp

示例2: savePage

int wmtor::savePage(HttpContent &content, HttpHeader &header) {
    try {
        ofstream headerFile("header.txt", ios::app);
        ofstream contentFile("content.txt", ios::app);
        if (headerFile.is_open() && contentFile.is_open()) {
            headerFile << header.getHeaderStr() << endl;
            contentFile << content.getContentStr() << endl;
        }
        if (headerFile.is_open())headerFile.close();
        if (contentFile.is_open())contentFile.close();
        return 0;
    } catch (exception* e) {
        cerr << e->what() << endl;
        return -1;
    }
}
开发者ID:,项目名称:,代码行数:16,代码来源:

示例3: CheckUrl_wholePage

void wmtor::CheckUrl_wholePage(Url &url, HttpClient&cli, ofstream &ofile) {
    HttpHeader header;
    HttpContent content;
    cli.requestWebPage(url, header, content);
    ofile << "url:" << url.getUrlStr() << endl;
    ofile << "header:" << header.getHeaderStr() << endl;
    cout << "url:" << url.getUrlStr() << endl;
    cout << "urlEtag:" << header.getEtag() << endl;
    string contentMD5 = content.getMD5Str();

    if ( !contentMD5.empty() && contentMD5 != url.getPageMD5Str()) {
        url.setEtag(header.getEtag());
        url.setAge(0);
        url.setPageMD5Str(contentMD5);
        saveNewPage(url,content, header);
        reportNewWebPage(url);
    }
}
开发者ID:,项目名称:,代码行数:18,代码来源:

示例4: CheckUrl

void wmtor::CheckUrl(Url &url, HttpClient&cli, ofstream &ofile) {
    HttpHeader header;
    cli.requestHeader(url, header);
    ofile << "url:" << url.getUrlStr() << endl;
    ofile << "header:" << header.getHeaderStr() << endl;
    cout << "url:" << url.getUrlStr() << endl;
    cout << "urlEtag:" << header.getEtag() << endl;
    if (header.getEtag() != "") {
        if (header.getEtag() != url.getEtag()) {
            HttpContent content;
            cli.requestContent(url, content, header.getContentLength());
            string contentMD5 = content.getMD5Str();
            url.setEtag(header.getEtag());
            url.setAge(header.getAge());
            url.setPageMD5Str(contentMD5);
            savePage(content, header);
            reportNewWebPage(url);
        }
    } else {
    }
}
开发者ID:,项目名称:,代码行数:21,代码来源:

示例5: requestWebPage

int HttpClient::requestWebPage(Url &url, HttpHeader &httpHeader, HttpContent &httpContent) {
    // construct a request
    string requestStr;
    string path = url.getPath();
    if (path.empty())
        path = "/";
    requestStr = "GET " + path + " HTTP/1.0\r\nHost: " + url.getHost()
            + "\r\nUser-Agent: openSE/1.0 (Ubuntu11.04)\r\nAccept-Language: zh,en-us\r\nAccept-Charset: gb2312,utf-8\r\nConnection: Keep-Alive\r\n\r\n";

    cout << "requestStr:\n" << requestStr << endl;

    // send request:
    if (url.getHost() != _preHost) {
        if (_preSockFd != -1) {
            closesocket(_preSockFd);
            _preSockFd = -1;
        }
    }
    int sockFd;
    bool sendSuccess = false;

    // try to use previous connection
    if (_preSockFd != -1) {
        sockFd = _preSockFd;
        if (rio_writen(sockFd, requestStr.c_str(), requestStr.size()) == -1) {
            cerr << "use previous connection:rio_writen error !" << endl;
            closesocket(_preSockFd);
            _preSockFd = -1;
        } else
            sendSuccess = true;
    }

    if (!sendSuccess) {
        // try to creat a new connection
        sockFd = tcpConnect(url.getIp(), url.getPort());
        if (sockFd == -1) {
            cerr << "tcpConnect error" << endl;
            return -1;
        }
        // try to use new connection
        if (rio_writen(sockFd, requestStr.c_str(), requestStr.size()) == -1) {
            cerr << "rio_writen error for requestStr:" << requestStr << endl;
            closesocket(sockFd);
            return -1;
        }
    }


    // receive http header
    string headerStr;
    headerStr.reserve(1024);
    if (receiveHeader(sockFd, headerStr, DEFAULT_TIMEOUT_SECONDS) <= 0) {
        cerr << "receiveHeader error" << endl;
        closesocket(sockFd);
        _preSockFd = -1;
        return -1;
    }

    //cout << "headerStr:\n" << headerStr << endl;

    // parser http header
    httpHeader.setHeaderStr(headerStr);

    // check StatusCode
    int stausCode = httpHeader.getStatusCode();
    if (stausCode == -1) {
        cerr << "not find status code in httpHeader: " << httpHeader.getHeaderStr() << endl;
    }

    if (stausCode == 301 || stausCode == 302) {
        closesocket(sockFd);
        _preSockFd = -1;
        string locationUrlStr = httpHeader.getLocation();
        if (locationUrlStr.empty()) {
            cerr << "error location in httpHeader: " << httpHeader.getHeaderStr() << endl;
        }
        //locationStr = location;
        Url locationUrl(locationUrlStr);
        return requestWebPage(locationUrl, httpHeader, httpContent);
    }

    if (stausCode < 200 || stausCode > 299) {
        closesocket(sockFd);
        _preSockFd = -1;
        cerr << "status code beyond [200-300) in httpHeader: " << httpHeader.getHeaderStr() << endl;
        return -1;
    }

    // check content type
    string contentType = httpHeader.getContentType();
    if (contentType.find("image") != string::npos) {
        closesocket(sockFd);
        _preSockFd = -1;
        cerr << "contentType is image in httpHeader: " << httpHeader.getHeaderStr() << endl;
        return -1;
    }

    // check ContentLength
    int contentLength = httpHeader.getContentLength();

//.........这里部分代码省略.........
开发者ID:skiloop,项目名称:wmtor,代码行数:101,代码来源:HttpClient.cpp


注:本文中的HttpHeader::getHeaderStr方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。