代码拉取完成,页面将自动刷新
/**
* @author : xiaozhuai
* @date : 17/1/4
*/
#include <iostream>
#include <sstream>
#include <cxxurl/cxxurl_all.h>
using namespace std;
using namespace CXXUrl;
void example_simple_form();
void example_multipart_form();
void example_raw_body_text();
void example_raw_body_binary();
int main(int argc, char** argv){
example_simple_form();
example_multipart_form();
example_raw_body_text();
example_raw_body_binary();
}
void example_simple_form(){
ostringstream contentOutput;
// simple form, you can only set key-value (x-www-form-urlencoded)
SimpleForm form;
form.add("name", "xiaozhuai");
form.add("sex", "male");
Request request = RequestBuilder()
.url("http://localhost:3000/post")
.followLocation(true)
.requestBody(&form)
.contentOutput(&contentOutput)
.build();
CURLcode res = request.post();
cout << "------------ Code ------------" << endl
<< res << endl
<< "----------- Content ----------" << endl
<< contentOutput.str() << endl
<< flush;
}
void example_multipart_form(){
ostringstream contentOutput;
// multipart form, you can upload a file, or key-value
MultipartForm form;
form.add("name", "xiaozhuai");
form.addFile("avatar", "../example_server/tao.png");
form.addFile("avatar2", "../example_server/tao.png", "tao2.png");
Request request = RequestBuilder()
.url("http://localhost:3000/post")
.followLocation(true)
.requestBody(&form)
.contentOutput(&contentOutput)
.build();
CURLcode res = request.post();
cout << "------------ Code ------------" << endl
<< res << endl
<< "----------- Content ----------" << endl
<< contentOutput.str() << endl
<< flush;
}
void example_raw_body_text(){
ostringstream contentOutput;
// raw form, you can set request body with text
RawRequestBody form;
form.setRawText("{ \"name\": \"xiaozhuai\" }");
Request request = RequestBuilder()
.url("http://localhost:3000/post")
.followLocation(true)
.requestBody(&form)
.contentType("application/json")
.contentOutput(&contentOutput)
.build();
CURLcode res = request.post();
cout << "------------ Code ------------" << endl
<< res << endl
<< "----------- Content ----------" << endl
<< contentOutput.str() << endl
<< flush;
}
void example_raw_body_binary(){
ostringstream contentOutput;
// raw form, you can set request body with raw bytes
char buffer[100];
RawRequestBody form;
form.setRawData(buffer, 100);
Request request = RequestBuilder()
.url("http://localhost:3000/post")
.followLocation(true)
.requestBody(&form)
.contentType("application/octet-stream")
.contentOutput(&contentOutput)
.build();
CURLcode res = request.post();
cout << "------------ Code ------------" << endl
<< res << endl
<< "----------- Content ----------" << endl
<< contentOutput.str() << endl
<< flush;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。