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

Y.A.K.E 2013-06-26 02:04:46
定义一个函数

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可以用缓冲区..


另外求代理设置方法
...全文
204 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
旋转的棍子 2013-07-12
  • 打赏
  • 举报
回复
数据已经写到 char wr_buf[MAX_BUF+1]; 里面了啊,你直接用就是了
旋转的棍子 2013-07-11
  • 打赏
  • 举报
回复
引用 5 楼 lovecuicui13 的回复:

#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;
旋转的棍子 2013-07-11
  • 打赏
  • 举报
回复

#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;
}
Y.A.K.E 2013-07-11
  • 打赏
  • 举报
回复
引用 6 楼 lovecuicui13 的回复:
[quote=引用 5 楼 lovecuicui13 的回复:]

#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;[/quote] 请问如果希望test()返回呢.网页内容呢
Y.A.K.E 2013-07-09
  • 打赏
  • 举报
回复
引用 3 楼 lovecuicui13 的回复:
[quote=引用 2 楼 xunni1000 的回复:] [quote=引用 1 楼 qq120848369 的回复:] 注册writefunction.
这个必须使用回调函数吧...[/quote]你回调的时候,不写文件,而是把数据写进你的缓存区就行了[/quote] 缓存区好高级的样子..不会啊,大侠
旋转的棍子 2013-07-01
  • 打赏
  • 举报
回复
引用 2 楼 xunni1000 的回复:
[quote=引用 1 楼 qq120848369 的回复:] 注册writefunction.
这个必须使用回调函数吧...[/quote]你回调的时候,不写文件,而是把数据写进你的缓存区就行了
Y.A.K.E 2013-06-26
  • 打赏
  • 举报
回复
引用 1 楼 qq120848369 的回复:
注册writefunction.
这个必须使用回调函数吧...
qq120848369 2013-06-26
  • 打赏
  • 举报
回复
注册writefunction.

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧