1 #include <iostream> 2 #include <cstdlib> 3 #include <cstring> 4 #include "../utils.h" 5 #include "alibabacloud/core/AlibabaCloud.h" 6 #include "alibabacloud/core/CommonClient.h" 7 8 using namespace std; 9 using namespace AlibabaCloud; 10 main(int argc,char ** argv)11int main(int argc, char** argv) { 12 utUtils utils; 13 string key = utils.get_env("ENV_AccessKeyId"); 14 string secret = utils.get_env("ENV_AccessKeySecret"); 15 16 17 InitializeSdk(); 18 ClientConfiguration configuration("cn-shanghai"); 19 CommonClient client(key, secret, configuration); 20 21 CommonRequest request(CommonRequest::RoaPattern); 22 request.setScheme("http"); 23 request.setDomain("nlp.cn-shanghai.aliyuncs.com"); 24 request.setResourcePath("/nlp/api/wordsegment/general"); 25 request.setHttpMethod(HttpRequest::Post); 26 27 // if windows, Chinese character will result in build error. 28 const std::string data = "{\"lang\":\"ZH\",\"text\":\"Iphone is a good choice.\"}"; 29 30 request.setContent(data.c_str(), data.size()); 31 request.setHeaderParameter("Content-Type", "application/json;chrset=utf-8"); 32 request.setHeaderParameter("Accept", "application/json"); 33 request.setVersion("2018-04-08"); 34 35 auto out = client.commonResponse(request); 36 if (!out.isSuccess()) { 37 cout << "error code: " << out.error().errorCode() << endl; 38 cout << "error message: " << out.error().errorMessage() << endl; 39 cout << "error host: " << out.error().host() << endl; 40 cout << "error requestId: " << out.error().requestId() << endl; 41 cout << "error detail: " << out.error().detail() << endl; 42 ShutdownSdk(); 43 return -1; 44 } 45 cout << endl << "wordsegment retruns: " << out.result().payload() << std::endl << endl; 46 ShutdownSdk(); 47 return 0; 48 } 49