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


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

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


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

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