首页 > 数据库 > mysql教程 > Cocos2d-x中libcurl库的使用(3)HTTP的介绍

Cocos2d-x中libcurl库的使用(3)HTTP的介绍

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
发布: 2016-06-07 15:01:06
原创
1163 人浏览过

1、HTTP验证 HTTP目前支持的验证方法有:basic、Digest、NTLM、Negotiate、GSS-Negotiate、SPENGO,可以通过CURLOPT_HTTPAUTH属性来设置具体的验证方式,如:curl_easy_setopt(easy_handle,CURLOPT_HTTPAUTH,CURLAUTH_DIGEST);向代理服务器发送验证信息时,

1、HTTP验证

HTTP目前支持的验证方法有:basic、Digest、NTLM、Negotiate、GSS-Negotiate、SPENGO,可以通过CURLOPT_HTTPAUTH属性来设置具体的验证方式,如:curl_easy_setopt(easy_handle,CURLOPT_HTTPAUTH,CURLAUTH_DIGEST);向代理服务器发送验证信息时,可以通过CURLOPT_PROXYAUTH设置验证方式,也可以同时设置多种验证方式,通过CURLOPT_HTTPAUTH或CURLOPT_PROXYAUTH属性设置多种验证方式。curl_easy_setopt(easy_handle,CURLOPT_PROXYAUTH,CURLAUTH_NTLM)

libcurl在运行时会选择一种它认为是最好的方式与服务器通信。

2、libcurl之HTTP Post

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

#include <stdio.h>

#include <curl>

 

int main(void)

{

  CURL *curl;

  CURLcode res;

 

  /* In windows, this will init the winsock stuff */

  curl_global_init(CURL_GLOBAL_ALL);

 

  /* get a curl handle */

  curl = curl_easy_init();

  if(curl) {

    /* First set the URL that is about to receive our POST. This URL can

       just as well be a https:// URL if that is what should receive the

       data. */

    curl_easy_setopt(curl, CURLOPT_URL, "http://postit.example.com/moo.cgi");

    /* Now specify the POST data */

    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=daniel&project=curl");

 

    /* Perform the request, res will get the return code */

    res = curl_easy_perform(curl);

    /* Check for errors */

    if(res != CURLE_OK)

      fprintf(stderr, "curl_easy_perform() failed: %s\n",

              curl_easy_strerror(res));

 

    /* always cleanup */

    curl_easy_cleanup(curl);

  }

  curl_global_cleanup();

  return 0;

}</curl></stdio.h>

登录后复制

3、libcurl之Multi Post

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

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

#include <stdio.h>

#include <string.h>

#include <sys>

 

#include <curl>

 

int main(void)

{

  CURL *curl;

 

  CURLM *multi_handle;

  int still_running;

 

  struct curl_httppost *formpost=NULL;

  struct curl_httppost *lastptr=NULL;

  struct curl_slist *headerlist=NULL;

  static const char buf[] = "Expect:";

 

  /* Fill in the file upload field. This makes libcurl load data from

     the given file name when curl_easy_perform() is called. */

  curl_formadd(&formpost,

               &lastptr,

               CURLFORM_COPYNAME, "sendfile",

               CURLFORM_FILE, "postit2.c",

               CURLFORM_END);

 

  /* Fill in the filename field */

  curl_formadd(&formpost,

               &lastptr,

               CURLFORM_COPYNAME, "filename",

               CURLFORM_COPYCONTENTS, "postit2.c",

               CURLFORM_END);

 

  /* Fill in the submit field too, even if this is rarely needed */

  curl_formadd(&formpost,

               &lastptr,

               CURLFORM_COPYNAME, "submit",

               CURLFORM_COPYCONTENTS, "send",

               CURLFORM_END);

 

  curl = curl_easy_init();

  multi_handle = curl_multi_init();

 

  /* initalize custom header list (stating that Expect: 100-continue is not

     wanted */

  headerlist = curl_slist_append(headerlist, buf);

  if(curl && multi_handle) {

 

    /* what URL that receives this POST */

    curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/upload.cgi");

    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

 

    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);

    curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);

 

    curl_multi_add_handle(multi_handle, curl);

 

    curl_multi_perform(multi_handle, &still_running);

 

    do {

      struct timeval timeout;

      int rc; /* select() return code */

 

      fd_set fdread;

      fd_set fdwrite;

      fd_set fdexcep;

      int maxfd = -1;

 

      long curl_timeo = -1;

 

      FD_ZERO(&fdread);

      FD_ZERO(&fdwrite);

      FD_ZERO(&fdexcep);

 

      /* set a suitable timeout to play around with */

      timeout.tv_sec = 1;

      timeout.tv_usec = 0;

 

      curl_multi_timeout(multi_handle, &curl_timeo);

      if(curl_timeo >= 0) {

        timeout.tv_sec = curl_timeo / 1000;

        if(timeout.tv_sec > 1)

          timeout.tv_sec = 1;

        else

          timeout.tv_usec = (curl_timeo % 1000) * 1000;

      }

 

      /* get file descriptors from the transfers */

      curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);

 

      /* In a real-world program you OF COURSE check the return code of the

         function calls.  On success, the value of maxfd is guaranteed to be

         greater or equal than -1.  We call select(maxfd + 1, ...), specially in

         case of (maxfd == -1), we call select(0, ...), which is basically equal

         to sleep. */

 

      rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);

 

      switch(rc) {

      case -1:

        /* select error */

        break;

      case 0:

      default:

        /* timeout or readable/writable sockets */

        printf("perform!\n");

        curl_multi_perform(multi_handle, &still_running);

        printf("running: %d!\n", still_running);

        break;

      }

    } while(still_running);

 

    curl_multi_cleanup(multi_handle);

 

    /* always cleanup */

    curl_easy_cleanup(curl);

 

    /* then cleanup the formpost chain */

    curl_formfree(formpost);

 

    /* free slist */

    curl_slist_free_all (headerlist);

  }

  return 0;

}</curl></sys></string.h></stdio.h>

登录后复制

Multi Post可以提交二进制数据(或大量数据)的更好的方法,在RFC1867、RFC2388找到定义,在Post时,有不同的数据单元,每个单元有自己的名称与内容,内容可以是文本的,也可以是二进制的,同时,每个数据单元都可以有自己的消息头。这些数据单元组成一个链表,提交到HTTP服务器,也可以添加不同的数据单元,然后提交到服务器。
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
cocos2d-x - 在MVC模式中如何设置计时器?
来自于 1970-01-01 08:00:00
0
0
0
c++ - cocos2d没法下载?求助!!!
来自于 1970-01-01 08:00:00
0
0
0
nginx - X-Forwarded-For 和 X-Real-IP 的区别?
来自于 1970-01-01 08:00:00
0
0
0
关于本节课中 x-=y 的问题
来自于 1970-01-01 08:00:00
0
0
0
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板