Chinaunix首页 | 论坛 | 博客
  • 博客访问: 451233
  • 博文数量: 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:24:28


点击(此处)折叠或打开

  1. #ifndef LTP_REQUEST_H
  2. #define LTP_REQUEST_H
  3. #include <string>
  4. #include <map>
  5. #include <curl/curl.h>

  6. namespace ns_ltp{

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


  12.         CURL *curl;

  13.     public:
  14.         LtpRequest();
  15.         ~LtpRequest();

  16.         bool Init(char *errorBuffer, size_t bufferLength);

  17.         void Reset();

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

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

  20.         size_t ConsumeData(char *dataBuffer, size_t size, size_t nmemb);
  21. };

  22. extern LtpRequest ltpRequest;

  23. };
  24. #endif


点击(此处)折叠或打开

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

  8. using namespace std;
  9. using namespace ns_ltp;

  10. LtpRequest ns_ltp::ltpRequest;

  11. size_t maxBufferLength = 1024 * 1024;

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

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

  17.     currentLength = 0;

  18.     curl = NULL;
  19. }

  20. LtpRequest::~LtpRequest()
  21. {
  22.     if (buffer)
  23.     {
  24.         free(buffer);
  25.     }
  26. }

  27. bool LtpRequest::Init(char *errorBuffer, size_t bufferLength)
  28. {
  29.     if ((!errorBuffer) || (!bufferLength))
  30.         return false;

  31.     curl = curl_easy_init();
  32.     if (!curl)
  33.     {
  34.         //snprintf(errorBuffer, bufferLength, "failed to init curl");
  35.         snprintf(errorBuffer, bufferLength, "failed to init curl\n");
  36.         return false;
  37.     }

  38.     static string url = "";
  39.     curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); //url地址
  40.     curl_easy_setopt(curl, CURLOPT_POST, 1); //设置问非0表示本次操作为post

  41.     return true;
  42. }

  43. void LtpRequest::Reset()
  44. {
  45.     currentLength = 0;
  46.     memset(buffer, 0, maxBufferLength);
  47. }

  48. bool LtpRequest::RequestAll(std::string &text, std::string &response, std::string &errorString)
  49. {
  50.     //static string ltpPattern("all");
  51.     //return Request(text, ltpPattern, response, errorString);
  52.     return false;
  53. }

  54. bool LtpRequest::Request(std::string &text, std::string &ltpPattern, std::string &response, char *errorBuffer, size_t bufferLength)
  55. {
  56.     if ((!errorBuffer) || (!bufferLength))
  57.         return false;
  58.     if (text.empty())
  59.     {
  60.         snprintf(errorBuffer, bufferLength, "text missed");
  61.         return false;
  62.     }

  63.     string newText;
  64.     char *s = curl_easy_escape(NULL, text.c_str(), text.size());
  65.     if (s)
  66.     {
  67.         newText = string(s);
  68.         curl_free(s);
  69.     }
  70.     else
  71.     {
  72.         snprintf(errorBuffer, bufferLength, "failed to call CurlTranformText\n");
  73.         return false;
  74.     }

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

  76.     string myKey;
  77.     string fields = "api_key=" + myKey;
  78.     fields += "&text=" + newText;
  79.     fields += "&pattern=" + ltpPattern;
  80.     fields += "&format=json";

  81.     //printf("%s\n", url.c_str());
  82.     //printf("%s\n", fields.c_str());

  83.     
  84.     //bool result = false;
  85.     //result = GetUrl(url.c_str(), response, errorString);

  86.     CURLcode res;
  87.     curl_easy_setopt(curl, CURLOPT_POSTFIELDS, fields.c_str()); //post参数

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

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

  95.     res = curl_easy_perform(curl);

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

  114.     if (currentLength == 0)
  115.     {
  116.         curl_easy_cleanup(curl);
  117.         return false;
  118.     }

  119.     response = string(buffer);
  120.     //curl_easy_cleanup(curl);
  121.     return true;
  122. }

  123. size_t LtpRequest::ConsumeData(char *dataBuffer, size_t size, size_t nmemb)
  124. {
  125.     size_t totalCount = size * nmemb;
  126.     if (!totalCount)
  127.     {
  128.         //printf("failed ,total count is zero\n");    
  129.         return 0;
  130.     }

  131.     if (currentLength + totalCount >= maxBufferLength)
  132.     {
  133.         printf("ltp request memory not enough\n");
  134.         abort();
  135.     }

  136.     memcpy(buffer + currentLength, dataBuffer, totalCount);
  137.     currentLength += totalCount;

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

  139.     return totalCount;
  140. }

  141. size_t write_data(void *buffer, size_t size, size_t nmemb, void *tmp)
  142. {
  143.     LtpRequest *request = (LtpRequest *)tmp;
  144.     return request->ConsumeData((char *)buffer, size, nmemb);
  145. }

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