本文整理汇总了C++中HTTPResponse::get方法的典型用法代码示例。如果您正苦于以下问题:C++ HTTPResponse::get方法的具体用法?C++ HTTPResponse::get怎么用?C++ HTTPResponse::get使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HTTPResponse
的用法示例。
在下文中一共展示了HTTPResponse::get方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: getUrl
ofxHttpResponse ofxHttpUtils::getUrl(string url){
ofxHttpResponse response;
try{
URI uri(url.c_str());
std::string path(uri.getPathAndQuery());
if (path.empty()) path = "/";
HTTPClientSession session(uri.getHost(), uri.getPort());
HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
if(auth.getUsername()!="") auth.authenticate(req);
session.setTimeout(Poco::Timespan(timeoutSeconds,0));
session.sendRequest(req);
HTTPResponse res;
istream& rs = session.receiveResponse(res);
response=ofxHttpResponse(res, rs, path);
if(response.status>=300 && response.status<400){
Poco::URI uri(req.getURI());
uri.resolve(res.get("Location"));
response.location = uri.toString();
}
ofNotifyEvent( newResponseEvent, response, this );
//std::cout << res.getStatus() << " " << res.getReason() << std::endl;
//StreamCopier::copyStream(rs, std::cout);
}catch (Exception& exc){
std::cerr << exc.displayText() << "\n";
}
return response;
}
示例2: processRelocation
int InternetHelper::processRelocation(const HTTPResponse &response,
std::ostream &responseStream) {
std::string newLocation = response.get("location", "");
if (!newLocation.empty()) {
g_log.information() << "url relocated to " << newLocation;
return this->sendRequest(newLocation, responseStream);
} else {
g_log.warning("Apparent relocation did not give new location\n");
return response.getStatus();
}
}
示例3: getUrl
// ----------------------------------------------------------------------
ofxHttpResponse ofxHttpUtils::getUrl(string url){
ofxHttpResponse response;
try{
URI uri(url.c_str());
std::string path(uri.getPathAndQuery());
if (path.empty()) path = "/";
HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
if(auth.getUsername()!="") auth.authenticate(req);
if(sendCookies){
for(unsigned i=0; i<cookies.size(); i++){
NameValueCollection reqCookies;
reqCookies.add(cookies[i].getName(),cookies[i].getValue());
req.setCookies(reqCookies);
}
}
HTTPResponse res;
ofPtr<HTTPSession> session;
istream * rs;
if(uri.getScheme()=="https"){
HTTPSClientSession * httpsSession = new HTTPSClientSession(uri.getHost(), uri.getPort());//,context);
httpsSession->setTimeout(Poco::Timespan(timeoutSeconds,0));
httpsSession->sendRequest(req);
rs = &httpsSession->receiveResponse(res);
session = ofPtr<HTTPSession>(httpsSession);
}else{
HTTPClientSession * httpSession = new HTTPClientSession(uri.getHost(), uri.getPort());
httpSession->setTimeout(Poco::Timespan(timeoutSeconds,0));
httpSession->sendRequest(req);
rs = &httpSession->receiveResponse(res);
session = ofPtr<HTTPSession>(httpSession);
}
response=ofxHttpResponse(res, *rs, url);
if(sendCookies){
cookies.insert(cookies.begin(),response.cookies.begin(),response.cookies.end());
}
if(response.status>=300 && response.status<400){
Poco::URI uri(req.getURI());
uri.resolve(res.get("Location"));
response.location = uri.toString();
}
ofNotifyEvent( newResponseEvent, response, this );
//std::cout << res.getStatus() << " " << res.getReason() << std::endl;
//StreamCopier::copyStream(rs, std::cout);
}catch (Exception& exc){
ofLogError("ofxHttpUtils") << exc.displayText();
response.status = -1;
response.reasonForStatus = exc.displayText();
ofNotifyEvent(newResponseEvent, response, this);
}
return response;
}
示例4: doPostForm
// ----------------------------------------------------------------------
ofxHttpResponse ofxHttpUtils::doPostForm(ofxHttpForm & form){
ofxHttpResponse response;
try{
URI uri( form.action.c_str() );
std::string path(uri.getPathAndQuery());
if (path.empty()) path = "/";
//HTTPClientSession session(uri.getHost(), uri.getPort());
HTTPRequest req(HTTPRequest::HTTP_POST, path, HTTPMessage::HTTP_1_1);
if(auth.getUsername()!="") auth.authenticate(req);
if(sendCookies){
for(unsigned i=0; i<cookies.size(); i++){
NameValueCollection reqCookies;
reqCookies.add(cookies[i].getName(),cookies[i].getValue());
req.setCookies(reqCookies);
}
}
for (unsigned int i = 0; i < form.headerIds.size(); ++i) {
const std::string name = form.headerIds[i].c_str();
const std::string val = form.headerValues[i].c_str();
req.set(name, val);
}
HTTPResponse res;
HTMLForm pocoForm;
// create the form data to send
if(form.formFiles.size()>0) {
pocoForm.setEncoding(HTMLForm::ENCODING_MULTIPART);
}
else {
pocoForm.setEncoding(HTMLForm::ENCODING_URL);
}
// form values
for(unsigned i=0; i<form.formIds.size(); i++){
const std::string name = form.formIds[i].c_str();
const std::string val = form.formValues[i].c_str();
pocoForm.set(name, val);
}
map<string,string>::iterator it;
for(it = form.formFiles.begin(); it!=form.formFiles.end(); it++){
string fileName = it->second.substr(it->second.find_last_of('/')+1);
ofLogVerbose("ofxHttpUtils") << "adding file: " << fileName << " path: " << it->second;
pocoForm.addPart(it->first,new FilePartSource(it->second));
}
pocoForm.prepareSubmit(req);
ofPtr<HTTPSession> session;
istream * rs;
if(uri.getScheme()=="https"){
HTTPSClientSession * httpsSession = new HTTPSClientSession(uri.getHost(), uri.getPort());//,context);
httpsSession->setTimeout(Poco::Timespan(20,0));
pocoForm.write(httpsSession->sendRequest(req));
rs = &httpsSession->receiveResponse(res);
session = ofPtr<HTTPSession>(httpsSession);
}else{
HTTPClientSession * httpSession = new HTTPClientSession(uri.getHost(), uri.getPort());
httpSession->setTimeout(Poco::Timespan(20,0));
pocoForm.write(httpSession->sendRequest(req));
rs = &httpSession->receiveResponse(res);
session = ofPtr<HTTPSession>(httpSession);
}
response = ofxHttpResponse(res, *rs, form.action);
if(sendCookies){
cookies.insert(cookies.begin(),response.cookies.begin(),response.cookies.end());
}
if(response.status>=300 && response.status<400){
Poco::URI uri(req.getURI());
uri.resolve(res.get("Location"));
response.location = uri.toString();
}
ofNotifyEvent(newResponseEvent, response, this);
}catch (Exception& exc){
ofLogError("ofxHttpUtils") << "ofxHttpUtils error doPostForm -- " << form.action.c_str();
//ofNotifyEvent(notifyNewError, "time out", this);
// for now print error, need to broadcast a response
ofLogError("ofxHttpUtils") << exc.displayText();
response.status = -1;
response.reasonForStatus = exc.displayText();
ofNotifyEvent(newResponseEvent, response, this);
}
return response;
}
示例5: postData
// ----------------------------------------------------------------------
ofxHttpResponse ofxHttpUtils::postData(string url, const ofBuffer & data, string contentType){
ofxHttpResponse response;
try{
URI uri( url.c_str() );
std::string path(uri.getPathAndQuery());
if (path.empty()) path = "/";
//HTTPClientSession session(uri.getHost(), uri.getPort());
HTTPRequest req(HTTPRequest::HTTP_POST, path, HTTPMessage::HTTP_1_1);
if(auth.getUsername()!="") auth.authenticate(req);
if(sendCookies){
for(unsigned i=0; i<cookies.size(); i++){
NameValueCollection reqCookies;
reqCookies.add(cookies[i].getName(),cookies[i].getValue());
req.setCookies(reqCookies);
}
}
if(contentType!=""){
req.setContentType(contentType);
}
req.setContentLength(data.size());
HTTPResponse res;
ofPtr<HTTPSession> session;
istream * rs;
if(uri.getScheme()=="https"){
HTTPSClientSession * httpsSession = new HTTPSClientSession(uri.getHost(), uri.getPort());//,context);
httpsSession->setTimeout(Poco::Timespan(20,0));
httpsSession->sendRequest(req) << data;
rs = &httpsSession->receiveResponse(res);
session = ofPtr<HTTPSession>(httpsSession);
}else{
HTTPClientSession * httpSession = new HTTPClientSession(uri.getHost(), uri.getPort());
httpSession->setTimeout(Poco::Timespan(20,0));
httpSession->sendRequest(req) << data;
rs = &httpSession->receiveResponse(res);
session = ofPtr<HTTPSession>(httpSession);
}
response = ofxHttpResponse(res, *rs, url);
if(sendCookies){
cookies.insert(cookies.begin(),response.cookies.begin(),response.cookies.end());
}
if(response.status>=300 && response.status<400){
Poco::URI uri(req.getURI());
uri.resolve(res.get("Location"));
response.location = uri.toString();
}
ofNotifyEvent(newResponseEvent, response, this);
}catch (Exception& exc){
ofLogError("ofxHttpUtils") << "ofxHttpUtils error postData --";
//ofNotifyEvent(notifyNewError, "time out", this);
// for now print error, need to broadcast a response
ofLogError("ofxHttpUtils") << exc.displayText();
response.status = -1;
response.reasonForStatus = exc.displayText();
ofNotifyEvent(newResponseEvent, response, this);
}
return response;
}
示例6: open
std::istream* HTTPStreamFactory::open(const URI& uri)
{
poco_assert (uri.getScheme() == "http");
URI resolvedURI(uri);
URI proxyUri;
HTTPClientSession* pSession = 0;
HTTPResponse res;
bool retry = false;
bool authorize = false;
std::string username;
std::string password;
try
{
do
{
if (!pSession)
{
pSession = new HTTPClientSession(resolvedURI.getHost(), resolvedURI.getPort());
if (proxyUri.empty())
pSession->setProxy(_proxyHost, _proxyPort);
else
pSession->setProxy(proxyUri.getHost(), proxyUri.getPort());
pSession->setProxyCredentials(_proxyUsername, _proxyPassword);
}
std::string path = resolvedURI.getPathAndQuery();
if (path.empty()) path = "/";
HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
if (authorize)
{
HTTPCredentials::extractCredentials(uri, username, password);
HTTPCredentials cred(username, password);
cred.authenticate(req, res);
}
pSession->sendRequest(req);
std::istream& rs = pSession->receiveResponse(res);
bool moved = (res.getStatus() == HTTPResponse::HTTP_MOVED_PERMANENTLY ||
res.getStatus() == HTTPResponse::HTTP_FOUND ||
res.getStatus() == HTTPResponse::HTTP_SEE_OTHER ||
res.getStatus() == HTTPResponse::HTTP_TEMPORARY_REDIRECT);
if (moved)
{
resolvedURI.resolve(res.get("Location"));
if (!username.empty())
{
resolvedURI.setUserInfo(username + ":" + password);
}
throw URIRedirection(resolvedURI.toString());
}
else if (res.getStatus() == HTTPResponse::HTTP_OK)
{
return new HTTPResponseStream(rs, pSession);
}
else if (res.getStatus() == HTTPResponse::HTTP_USEPROXY && !retry)
{
// The requested resource MUST be accessed through the proxy
// given by the Location field. The Location field gives the
// URI of the proxy. The recipient is expected to repeat this
// single request via the proxy. 305 responses MUST only be generated by origin servers.
// only use for one single request!
proxyUri.resolve(res.get("Location"));
delete pSession; pSession = 0;
retry = true; // only allow useproxy once
}
else if (res.getStatus() == HTTPResponse::HTTP_UNAUTHORIZED && !authorize)
{
authorize = true;
retry = true;
Poco::NullOutputStream null;
Poco::StreamCopier::copyStream(rs, null);
}
else throw HTTPException(res.getReason(), uri.toString());
}
while (retry);
throw HTTPException("Too many redirects", uri.toString());
}
catch (...)
{
delete pSession;
throw;
}
}