使用libcurl为webgame服务端增加http访问功能

星期三, 2013-01-30 | Author: Lee | webgame, 游戏开发 | 3,524 views

webgame的服务端代码使用c/c++开发,现在要使用http协议访问外网进行验证,为游戏增加这个功能,使用了比较出名的libcurl的库

用起来很方便,代码封装如下:

简介:1.安装curl的库 http://curl.haxx.se/libcurl/

2.对应的功能示例也蛮丰富的(自己根据实际需求改造了下)

3.编译下面的代码

1
g++ -l curl -o tcurl sq_curl.c


代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include        <stdio.h>
#include        <string.h>
#include        <curl/curl.h>
 
#define MAX_BUF 65536
 
char wr_buf[MAX_BUF+1];
int      wr_index;
 
size_t write_data(void* buffer,size_t size,size_t nmenb,void *userdata)
{
        int segsize     =       size*nmenb;
        if(wr_index+segsize > MAX_BUF)
        {
                *(int *) userdata=1;
                return 0;
        }
        memcpy((void*)&wr_buf[wr_index],buffer,(size_t)segsize);
        wr_index        +=      segsize;
        wr_buf[wr_index]        =0;
        return segsize;
}
 
char* sq_curl_url_get_data(const char * url)
{
        CURL *curl;
        CURLcode        res;
        int wr_error;
        wr_error        =       0;
        wr_index        =       0;
 
        curl  = curl_easy_init();
        if (curl)
        {
                curl_easy_setopt(curl, CURLOPT_URL,url);
                curl_easy_setopt(curl,CURLOPT_TIMEOUT,60);//60s
                curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
                curl_easy_setopt(curl,CURLOPT_WRITEDATA,(void*)&wr_error);
                curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,write_data);
 
                res     =       curl_easy_perform(curl);
 
                static char out[MAX_BUF+1]      =       {0};
                if(res==CURLE_OK)
                {
                        long code=0;
                        CURLcode ret  =         curl_easy_getinfo(curl,CURLINFO_RESPONSE_CODE,&code);
                        if(ret==CURLE_OK && code == 200)
                        {
                                strcpy(out,wr_buf);
                        }
                        else
                        {
                                printf("http request error code:%lu\n",code );
                                strcpy(out,"");
                        }
                }
                else
                {
                        printf("res=%d (write_error =%d),fail %s\n",res,wr_error,curl_easy_strerror(res));
                        strcpy(out,"");
                }
 
                curl_easy_cleanup(curl);
                //printf("%s\n",out);
 
                return  out;
        }
        return "";
}
 
int main(void)
{
       char * body= sq_curl_url_get_data("//www.pomelolee.com/ip.php");
        printf("%s\n", body);
}

Tags: , , ,

文章作者: Lee

本文地址: https://www.pomelolee.com/1130.html

除非注明,Pomelo Lee文章均为原创,转载请以链接形式标明本文地址

No comments yet.

Leave a comment

Search

文章分类

Meta