首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

求解libcurl取回来网页.以及代理设置方法

2013-07-16 
求解libcurl取返回网页.以及代理设置方法定义一个函数string getweb(url:string,proxy:string){//使得最后

求解libcurl取返回网页.以及代理设置方法
定义一个函数

string getweb(url:string,proxy:string)
{
//使得最后返回的是网页的HTML代码.错误则返回空.
    curl = curl_easy_init();        //初始化一个CURL类型的指针
    if(curl!=NULL)
    {

        curl_easy_setopt(curl, CURLOPT_URL, url);        
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
//....
}

不过我这样只能打印在屏幕上..不能作为返回.
网上翻了很多.只找到保存到文件的方法,而且必须要回调函数...

有没有直接可以返回的?
我记得以前PHP可以用缓冲区..


另外求代理设置方法
[解决办法]

引用:

#define MAX_BUF  65536
char wr_buf[MAX_BUF+1];
void test() 
{
    .....
    if (curl) {
 curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "d:/cookie.txt"); // 指定cookie文件
 curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "d:/cookie.txt");
 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers );
 curl_easy_setopt(curl, CURLOPT_URL, sign_url);   // 指定url
 curl_easy_setopt(curl, CURLOPT_READDATA, (void *)&wr_error);
 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data );//调用写回调

 /* Allow curl to perform the action */
 res = curl_easy_perform(curl);
 printf( "ret = %d (write_error = %d)\n", res, wr_error );

  /* Emit the page if curl indicates that no errors occurred */
 if ( res == 0 ) 
printf( "%s\n", wr_buf );
 curl_slist_free_all(headers);
 curl_easy_cleanup(curl);
}
    .....
}
/*
 * Write data callback function (called within the context of
 * curl_easy_perform.


 */
size_t write_data( void *buffer, size_t size, size_t nmemb, void *userp ) {
int segsize = size * nmemb;

  /* Check to see if this data exceeds the size of our buffer. If so,
   * set the user-defined context value and return 0 to indicate a
   * problem to curl.
   */
  if ( wr_index + segsize > MAX_BUF ) {
    *(int *)userp = 1;
    return 0;
  }

  /* Copy the data from the curl buffer into our buffer */
  memcpy( (void *)&wr_buf[wr_index], buffer, (size_t)segsize );

  /* Update the write index */
  wr_index += segsize;

  /* Null terminate the buffer */
  wr_buf[wr_index] = 0;

  /* Return the number of bytes received, indicating to curl that all is okay */
  return segsize;
}


前面还有个记录已经使用的位置的变量
int  wr_index=0;

热点排行