1
/**/
/*
************open_login_page.c**********
*/
2
#include
<
stdio.h
>
3
#include
<
string
.h
>
4
#include
<
curl
/
curl.h
>
5
#include
<
curl
/
types.h
>
6
#include
<
curl
/
easy.h
>
7
8
int
main(
int
argc,
char
*
argv[])
9
{
10
CURL
*
curl;
11
CURLcode res;
12
13
struct
curl_slist
*
headerlist
=
NULL;
14
static
const
char
buf[]
=
"
Expect:
"
;
15
16
curl_global_init(CURL_GLOBAL_ALL);
17
curl
=
curl_easy_init();
18
headerlist
=
curl_slist_append(headerlist, buf);
19
20
if
(curl)
21
{
22
curl_easy_setopt(curl, CURLOPT_URL,
"
https://passport.baidu.com/?login
"
);
23
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
24
curl_easy_setopt(curl, CURLOPT_COOKIEJAR,
"
cookie_open.txt
"
);
//
把服务器发过来的cookie保存到cookie_open.txt
25
26
#ifdef SKIP_PEER_VERIFICATION
27
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER,
0L
);
28
#endif
29
30
#ifdef SKIP_HOSTNAME_VERFICATION
31
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST,
0L
);
32
#endif
33
res
=
curl_easy_perform(curl);
34
35
/**/
/*
always cleanup
*/
36
curl_easy_cleanup(curl);
37
38
/**/
/*
free slist
*/
39
curl_slist_free_all(headerlist);
40
}
41
return
0
;
42
}
43
44
/**/
/*
*******************login_hi_baidu****************
*/
45
#include
<
stdio.h
>
46
#include
<
string
.h
>
47
#include
<
curl
/
curl.h
>
48
#include
<
curl
/
types.h
>
49
#include
<
curl
/
easy.h
>
50
51
int
main(
int
argc,
char
*
argv[])
52
{
53
CURL
*
curl;
54
CURLcode res;
55
56
struct
curl_slist
*
headerlist
=
NULL;
57
static
const
char
buf[]
=
"
Expect:
"
;
58
59
curl_global_init(CURL_GLOBAL_ALL);
60
curl
=
curl_easy_init();
61
headerlist
=
curl_slist_append(headerlist, buf);
62
63
if
(curl)
64
{
65
curl_easy_setopt(curl, CURLOPT_URL,
"
https://passport.baidu.com/?login
"
);
66
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
67
68
curl_easy_setopt(curl,CURLOPT_POSTFIELDS,
"
username=ea99&password=&psp_tt=0&safeflg=0&return_method=get&u=http://hi.baidu.com/s%5Fyqguo
"
);
//
设置帐号密码,其余的信息是页面要求的,抓包即可看见。
69
70
curl_easy_setopt(curl,CURLOPT_COOKIEFILE,
"
cookie_open.txt
"
);
//
提交第一步保存的cookie
71
curl_easy_setopt(curl,CURLOPT_COOKIEJAR,
"
cookie_login.txt
"
);
//
保存登陆后的cookie
72
#ifdef SKIP_PEER_VERIFICATION
73
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER,
0L
);
74
#endif
75
76
#ifdef SKIP_HOSTNAME_VERFICATION
77
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST,
0L
);
78
#endif
79
80
res
=
curl_easy_perform(curl);
81
/**/
/*
always cleanup
*/
82
curl_easy_cleanup(curl);
83
/**/
/*
free slist
*/
84
curl_slist_free_all(headerlist);
85
}
86
return
0
;
87
}
88
89
90
/**/
/*
***********download_private_page.c*****************
*/
91
#include
<
stdio.h
>
92
#include
<
string
.h
>
93
#include
<
curl
/
curl.h
>
94
#include
<
curl
/
types.h
>
95
#include
<
curl
/
easy.h
>
96
97
static
size_t write_data(
void
*
ptr, size_t size, size_t nmemb,
void
*
stream)
98
{
99
int
written
=
fwrite(ptr, size, nmemb, (FILE
*
)stream);
100
return
written;
101
}
102
103
int
main(
int
argc,
char
*
argv[])
104
{
105
CURL
*
curl;
106
CURLcode res;
107
108
static
const
char
*
headerfilename
=
"
head.out
"
;
109
FILE
*
headerfile;
110
111
static
const
char
*
bodyfilename
=
"
body.html
"
;
112
FILE
*
bodyfile;
113
114
struct
curl_slist
*
headerlist
=
NULL;
115
static
const
char
buf[]
=
"
Expect:
"
;
116
117
curl_global_init(CURL_GLOBAL_ALL);
118
curl
=
curl_easy_init();
119
headerlist
=
curl_slist_append(headerlist, buf);
120
if
(curl)
121
{
122
//
拉取私有页面
123
curl_easy_setopt(curl, CURLOPT_URL,
"
http://hi.baidu.com/ea99/blog/item/c4e99e58d7ec9d86800a18a2.html
"
);
124
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
125
126
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
127
128
headerfile
=
fopen(headerfilename,
"
w
"
);
129
if
(headerfile
==
NULL)
130
{
131
curl_easy_cleanup(curl);
132
curl_global_cleanup();
133
curl_slist_free_all(headerlist);
134
printf(
"
open head.out file failed!/n
"
);
135
return
-
1
;
136
}
137
138
bodyfile
=
fopen(bodyfilename,
"
w
"
);
139
if
(bodyfile
==
NULL)
140
{
141
curl_easy_cleanup(curl);
142
curl_global_cleanup();
143
curl_slist_free_all(headerlist);
144
printf(
"
open body.html file failed!/n
"
);
145
return
-
1
;
146
}
147
148
curl_easy_setopt(curl,CURLOPT_WRITEHEADER, headerfile);
149
curl_easy_setopt(curl,CURLOPT_WRITEDATA,bodyfile);
150
151
curl_easy_setopt(curl,CURLOPT_COOKIEFILE,
"
cookie_login.txt
"
);
//
把第二步保存的cookie发送给服务器验证
152
153
#ifdef SKIP_PEER_VERIFICATION
154
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER,
0L
);
155
#endif
156
157
#ifdef SKIP_HOSTNAME_VERFICATION
158
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST,
0L
);
159
#endif
160
res
=
curl_easy_perform(curl);
161
/**/
/*
always cleanup
*/
162
curl_easy_cleanup(curl);
163
/**/
/*
free slist
*/
164
curl_slist_free_all(headerlist);
165
curl_global_cleanup();
166
if
(headerfile
!=
NULL)
167
fclose(headerfile);
168
169
if
(bodyfile
!=
NULL)
170
fclose(bodyfile);
171
172
}
173
return
0
;
174
}
175
176
177
178
179
180


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

126

127

128

129

130



131

132

133

134

135

136

137

138

139

140



141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161


162

163


164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180
