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


C++ Url::getIp方法代码示例

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


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

示例1: requestContent

int HttpClient::requestContent(Url &url, string &contentStr, int expectContentLength) {
    // 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();

    //if(contentLength == -1){
//.........这里部分代码省略.........
开发者ID:skiloop,项目名称:wmtor,代码行数:101,代码来源:HttpClient.cpp

示例2: requestHeader

int HttpClient::requestHeader(Url &url, string&headerStr) {
    // construct a request
    string requestStr;
    string path = url.getPath();
    if (path.empty())
        path = "/";
    requestStr = "HEAD " + 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;
        }
    }


    headerStr.reserve(1024);
    int timeoutSeconds = DEFAULT_TIMEOUT_SECONDS;
    int bytesRead = 0;
    // set socket to no block type
#ifdef _MSC_VER

#else
    int flags;
    flags = fcntl(sockFd, F_GETFL, 0);
    if (flags < 0) {
        cerr << "1.fcntl() error in receiveHeader()< 0" << endl;
        return -1;
    }
    flags |= O_NONBLOCK;
    if (fcntl(sockFd, F_SETFL, flags) < 0) {
        cerr << "2.fcntl() error in receiveHeader" << endl;
        return -1;
    }
#endif

    int newlines = 0;
    while (newlines < 2) {
        fd_set rfds;
        FD_ZERO(&rfds);
        FD_SET(sockFd, &rfds);

        struct timeval tv;
        tv.tv_sec = timeoutSeconds;
        tv.tv_usec = 0;

        int selectRet;
        if (timeoutSeconds >= 0) // wait DEFAULT_TIMEOUT_SECONDS seconds
            selectRet = select(sockFd + 1, &rfds, NULL, NULL, &tv);
        else // wait infinitely
            selectRet = select(sockFd + 1, &rfds, NULL, NULL, NULL);

        if (selectRet == 0 && timeoutSeconds < 0) {
            cerr << "select should wait infinitely" << endl;
            bytesRead = -1;
            break;
        } else if (selectRet == -1) {
            cerr << "select error" << endl;
            bytesRead = -1;
            break;
        } else if (selectRet == 0) {
            cerr << "select timeout after " << timeoutSeconds << " seconds" << endl;
            bytesRead = -1;
            break;
        }

        char c;
        int ret = recv(sockFd, &c, 1, 0);
//.........这里部分代码省略.........
开发者ID:skiloop,项目名称:wmtor,代码行数:101,代码来源:HttpClient.cpp


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