Chinaunix首页 | 论坛 | 博客
  • 博客访问: 462690
  • 博文数量: 80
  • 博客积分: 2301
  • 博客等级: 大尉
  • 技术积分: 884
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-16 20:07
个人简介

I\'m interested in mathematics and Daoism. Welcome to talk about these subjects with me.

文章分类

全部博文(80)

文章存档

2017年(2)

2016年(16)

2015年(4)

2014年(6)

2013年(22)

2012年(2)

2011年(1)

2010年(4)

2009年(20)

2008年(2)

2007年(1)

我的朋友

分类: C/C++

2016-08-18 10:18:50


点击(此处)折叠或打开

  1. #ifndef LTP_REQUEST_H
  2. #define LTP_REQUEST_H
  3. #include <string>
  4. #include <map>

  5. namespace ns_ltp{

  6. //TODO:优化内存使用    
  7. class LtpRequest{
  8.     private:
  9.         char *buffer;
  10.         size_t currentLength;

  11.     public:
  12.         LtpRequest();
  13.         ~LtpRequest();

  14.         void Reset();

  15.         bool RequestAll(std::string &text, std::string &response, std::string &errorString);    

  16.         bool Request(std::string &text, std::string &ltpPattern, std::string &response, char *errorBuffer, size_t bufferLength);

  17.         size_t ConsumeData(char *dataBuffer, size_t size, size_t nmemb);
  18. };

  19. extern LtpRequest ltpRequest;

  20. };
  21. #endif

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string>
  3. #include <vector>
  4. #include "LtpRequest.h"
  5. #include <curl/curl.h>

  6. using namespace std;
  7. using namespace ns_ltp;

  8. LtpRequest ns_ltp::ltpRequest;

  9. size_t maxBufferLength = 1024 * 1024;

  10. static size_t write_data(void *buffer, size_t size, size_t nmemb, void *tmp);

  11. LtpRequest::LtpRequest()
  12. {
  13.     buffer = (char *)malloc(maxBufferLength);
  14.     memset(buffer, 0, maxBufferLength);

  15.     currentLength = 0;
  16. }

  17. LtpRequest::~LtpRequest()
  18. {
  19.     if (buffer)
  20.     {
  21.         free(buffer);
  22.     }
  23. }

  24. void LtpRequest::Reset()
  25. {
  26.     currentLength = 0;
  27.     memset(buffer, 0, maxBufferLength);
  28. }

  29. bool LtpRequest::RequestAll(std::string &text, std::string &response, std::string &errorString)
  30. {
  31.     //static string ltpPattern("all");
  32.     //return Request(text, ltpPattern, response, errorString);
  33.     return false;
  34. }

  35. bool LtpRequest::Request(std::string &text, std::string &ltpPattern, std::string &response, char *errorBuffer, size_t bufferLength)
  36. {
  37.     if ((!errorBuffer) || (!bufferLength))
  38.         return false;
  39.     if (text.empty())
  40.     {
  41.         snprintf(errorBuffer, bufferLength, "text missed");
  42.         return false;
  43.     }

  44.     string newText;
  45.     char *s = curl_easy_escape(NULL, text.c_str(), text.size());
  46.     if (s)
  47.     {
  48.         newText = string(s);
  49.         curl_free(s);
  50.     }
  51.     else
  52.     {
  53.         snprintf(errorBuffer, bufferLength, "failed to call CurlTranformText\n");
  54.         return false;
  55.     }

  56.     //printf("new text: %s\n", newText.c_str());


  57.     static string url = "";


  58.     string myKey;
  59.     string fields = "api_key=" + myKey;
  60.     fields += "&text=" + newText;
  61.     fields += "&pattern=" + ltpPattern;
  62.     fields += "&format=json";

  63.     //printf("%s\n", url.c_str());
  64.     //printf("%s\n", fields.c_str());

  65.     
  66.     //bool result = false;
  67.     //result = GetUrl(url.c_str(), response, errorString);

  68.     CURL *curl;
  69.     CURLcode res;

  70.     curl = curl_easy_init();
  71.     if (!curl)
  72.     {
  73.         snprintf(errorBuffer, bufferLength, "failed to init curl");
  74.         return false;
  75.     }

  76.     curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); //url地址
  77.     curl_easy_setopt(curl, CURLOPT_POST, 1); //设置问非0表示本次操作为post
  78.     
  79.     //如果是以上的初始化代码放在本类的初始化部分,下面的请求ltp的代码可以重复调用。
  80.     curl_easy_setopt(curl, CURLOPT_POSTFIELDS, fields.c_str()); //post参数

  81.     //对返回的数据进行操作的函数地址, 如果不设置,返回值打印到控制台
  82.     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
  83.     curl_easy_setopt(curl, CURLOPT_WRITEDATA, this); //这是write_data的第四个参数值

  84.     //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); //打印调试信息
  85.     //curl_easy_setopt(curl, CURLOPT_HEADER, 1); //将响应头信息和相应体一起传给write_data
  86.     //curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); //设置为非0,响应头信息location
  87.     //curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/Users/zhu/CProjects/curlposttest.cookie");

  88.     res = curl_easy_perform(curl);

  89.     if (res != CURLE_OK)
  90.     {
  91.         switch(res)
  92.         {
  93.             case CURLE_UNSUPPORTED_PROTOCOL:
  94.                 snprintf(errorBuffer, bufferLength, "不支持的协议,由URL的头部指定\n");
  95.             case CURLE_COULDNT_CONNECT:
  96.                 snprintf(errorBuffer, bufferLength, "不能连接到remote主机或者代理\n");
  97.             case CURLE_HTTP_RETURNED_ERROR:
  98.                 snprintf(errorBuffer, bufferLength, "http返回错误\n");
  99.             case CURLE_READ_ERROR:
  100.                 snprintf(errorBuffer, bufferLength, "读本地文件错误\n");
  101.             default:
  102.                 snprintf(errorBuffer, bufferLength ,"返回值:%d\n", res);
  103.         }
  104.         curl_easy_cleanup(curl);
  105.         return false;
  106.     }

  107.     if (currentLength == 0)
  108.     {
  109.         curl_easy_cleanup(curl);
  110.         return false;
  111.     }

  112.     response = string(buffer);
  113.     curl_easy_cleanup(curl);
  114.     return true;
  115. }

  116. size_t LtpRequest::ConsumeData(char *dataBuffer, size_t size, size_t nmemb)
  117. {
  118.     size_t totalCount = size * nmemb;
  119.     if (!totalCount)
  120.     {
  121.         //printf("failed ,total count is zero\n");    
  122.         return 0;
  123.     }

  124.     if (currentLength + totalCount >= maxBufferLength)
  125.     {
  126.         printf("ltp request memory not enough\n");
  127.         abort();
  128.     }

  129.     memcpy(buffer + currentLength, dataBuffer, totalCount);
  130.     currentLength += totalCount;

  131.     //printf("results are: %s\n", buffer);

  132.     return totalCount;
  133. }

  134. size_t write_data(void *buffer, size_t size, size_t nmemb, void *tmp)
  135. {
  136.     LtpRequest *request = (LtpRequest *)tmp;
  137.     return request->ConsumeData((char *)buffer, size, nmemb);
  138. }


阅读(651) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~