本文整理汇总了C++中HttpHeader::setHeaderStr方法的典型用法代码示例。如果您正苦于以下问题:C++ HttpHeader::setHeaderStr方法的具体用法?C++ HttpHeader::setHeaderStr怎么用?C++ HttpHeader::setHeaderStr使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpHeader
的用法示例。
在下文中一共展示了HttpHeader::setHeaderStr方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: requestHeader
int HttpClient::requestHeader(Url &url, HttpHeader &header) {
string headStr;
int ret = requestHeader(url, headStr);
header.setHeaderStr(headStr);
return ret;
}
示例2: 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();
//.........这里部分代码省略.........