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


C++ HTMLForm::setEncoding方法代码示例

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


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

示例1: doUpload

    //-------------------------------------------------------------
    string API::doUpload( string image ){        
        if ( !bAuthenticated ){
            ofLogWarning( "Not authenticated! Please call authenticate() with proper api key and secret" );
            return "";
        } else if ( currentPerms != FLICKR_WRITE ){
            ofLogWarning( "You do not have proper permissions to upload! Please call authenticate() with permissions of ofxFlickr::FLICKR_WRITE" );
            return "";
        }

        map<string,string> args;
        args["api_key"] = api_key;
        args["auth_token"] = auth_token;

        string result;

        FilePartSource * fps = new FilePartSource(image, "image/jpeg");

        try
        {

            // prepare session
            const URI uri( "https://" + api_base );
            HTTPSClientSession session( uri.getHost(), uri.getPort() );
            HTTPRequest req(HTTPRequest::HTTP_POST, "/services/upload/", HTTPMessage::HTTP_1_0);
            req.setContentType("multipart/form-data");

            // setup form
            HTMLForm form;
            form.set("api_key", api_key);
            form.set("auth_token", auth_token);
            form.set("api_sig", apiSig( args ));
            form.setEncoding(HTMLForm::ENCODING_MULTIPART);
            form.addPart("photo", fps);
            form.prepareSubmit(req);

            std::ostringstream oszMessage;
            form.write(oszMessage);
            std::string szMessage = oszMessage.str();

            req.setContentLength((int) szMessage.length() );

            //session.setKeepAlive(true);

            // send form
            ostream & out = session.sendRequest(req) << szMessage;

            // get response
            HTTPResponse res;
            cout << res.getStatus() << " " << res.getReason() << endl;

            // print response
            istream &is = session.receiveResponse(res);
            StreamCopier::copyToString(is, result);
        }
        catch (Exception &ex)
        {
            cerr << "error? " + ex.displayText() <<endl;
        }

        string photoid;

        ofxXmlSettings xml;
        xml.loadFromBuffer(result);
        xml.pushTag("rsp");{
            photoid = xml.getValue("photoid", "");
        }; xml.popTag();

        return photoid;
    }
开发者ID:robotconscience,项目名称:ofxFlickr,代码行数:70,代码来源:ofxFlickr.cpp

示例2: doPostForm

// ----------------------------------------------------------------------
int ofxHttpUtils::doPostForm(ofxHttpForm & form){
	int ret = -1;
    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);
        	}
        }

        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);
        	cout << "adding file: " << fileName << " path: " << it->second << endl;
        	pocoForm.addPart(it->first,new FilePartSource(it->second));
        }

        pocoForm.prepareSubmit(req);

        pocoForm.write(session.sendRequest(req));

        HTTPResponse res;
        istream& rs = session.receiveResponse(res);

		ofxHttpResponse response = ofxHttpResponse(res, rs, path);

		if(sendCookies){
			cookies.insert(cookies.begin(),response.cookies.begin(),response.cookies.end());
		}

    	ofNotifyEvent(newResponseEvent, response, this);

    	ret = 0;

    }catch (Exception& exc){

        printf("ofxHttpUtils error--\n");

        //ofNotifyEvent(notifyNewError, "time out", this);

        // for now print error, need to broadcast a response
        std::cerr << exc.displayText() << std::endl;

    }

    return ret;
}
开发者ID:heavyside,项目名称:refractiveindex,代码行数:71,代码来源:ofxHttpUtils.cpp

示例3: 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;
}
开发者ID:arturoc,项目名称:ofxHttpUtils,代码行数:100,代码来源:ofxHttpUtils.cpp

示例4: handleRequest

ofxHttpResponse ofxURLFileLoader::handleRequest(ofxHttpRequest request) {
	try {
		URI uri(request.url);
		std::string path(uri.getPathAndQuery());
		if (path.empty()) path = "/";

		HTTPClientSession session(uri.getHost(), uri.getPort());
        string method;
        switch (request.method) {
            case HTTP_METHOD_GET:
                method = HTTPRequest::HTTP_GET;
                break;
            case HTTP_METHOD_POST:
                method = HTTPRequest::HTTP_POST;
                break;
   
            default:
                method = HTTPRequest::HTTP_GET;
                break;
        }
        
		HTTPRequest req(method, path, HTTPMessage::HTTP_1_1);
		session.setTimeout(Poco::Timespan(20,0));
        
        if (!request.cookies.empty()) {
            NameValueCollection mvc;
//            cout << "request cookies:" << endl;
            for (vector<pair<string,string> >::iterator iter = request.cookies.begin();iter!=request.cookies.end();iter++) {
                mvc.add(iter->first, iter->second);
//                cout << iter->first << ": " << iter->second << endl;
                
            }
            req.setCookies(mvc);
            
        }
        
        if (request.nvc.empty() & request.files.empty()) {
            session.sendRequest(req);
        } else {
            HTMLForm pocoForm;
            // create the form data to send
            if(request.files.size()>0)
                pocoForm.setEncoding(HTMLForm::ENCODING_MULTIPART);
            else
                pocoForm.setEncoding(HTMLForm::ENCODING_URL);
            
            // form values
            for(unsigned i=0; i<request.nvc.size(); i++){
                const std::string name = request.nvc[i].first.c_str();
                const std::string val = request.nvc[i].second.c_str();
                pocoForm.set(name, val);
            }
            
            map<string,string>::iterator it;
            for(it = request.files.begin(); it!=request.files.end(); it++){
                string fileName = it->second.substr(it->second.find_last_of('/')+1);
                cout << "adding file: " << fileName << " path: " << it->second << endl;
                pocoForm.addPart(it->first,new FilePartSource(it->second));
            }
            
            pocoForm.prepareSubmit(req);
            
            pocoForm.write(session.sendRequest(req));
        }
        
        
        
        
        HTTPResponse res;
        istream& rs = session.receiveResponse(res);
        
        
        vector<HTTPCookie> pocoCookies;
        res.getCookies(pocoCookies);
        vector<pair<string,string> > cookies;
        
        //        res.write(cout);
        
        for (vector<HTTPCookie>::iterator iter=pocoCookies.begin();iter!=pocoCookies.end();iter++) {
            cookies.push_back(make_pair(iter->getName(), iter->getValue()));
        }
        
        if(!request.saveTo){
            
            
            
            return ofxHttpResponse(request,cookies,rs,res.getStatus(),res.getReason());
        }else{
            ofFile saveTo(request.name,ofFile::WriteOnly);
            char aux_buffer[1024];
            rs.read(aux_buffer, 1024);
            std::streamsize n = rs.gcount();
            while (n > 0){
                // we resize to size+1 initialized to 0 to have a 0 at the end for strings
                saveTo.write(aux_buffer,n);
                if (rs){
                    rs.read(aux_buffer, 1024);
                    n = rs.gcount();
                }
                else n = 0;
//.........这里部分代码省略.........
开发者ID:roikr,项目名称:openFrameworks007,代码行数:101,代码来源:ofxURLFileLoader.cpp


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