基于命令行的簡單的HTTP客戶端和服務器
1.實現HTTP 1.0 GET命令的客戶端 1
/**
2
*這是一個簡單的HTTP客戶端程序
3
* @author wangliang
4
*/
5
package http;
6
7
import java.io.BufferedInputStream;
8
import java.io.BufferedReader;
9
import java.net.Socket;
10
import java.io.FileOutputStream;
11
import java.io.IOException;
12
import java.io.InputStreamReader;
13
import java.io.PrintWriter;
14
import java.io.File;
15
import java.util.StringTokenizer;
16
17
/**
18
*這是一個簡單的HTTP客戶端程序,你可以實現HTTP1.0的GET命令
19
* @author wangliang
20
*/
21
22
23
public class Client {
24
25
public static void main(String args[]) throws Exception {
26
if (args.length == 2) {
27
Client client = new Client(args[0], Integer.parseInt(args[1]));
28
client.run();
29
} else if (args.length == 0) {
30
Client client2 = new Client("www.whu.edu.cn", 80);
31
client2.run();
32
} else {
33
System.out.println("ERROR.You should input as below");
34
System.out.println("java Client host port or java Client");
35
}
36
}
37
String host;
38
int port;
39
40
public Client(String host, int port) {
41
this.host = host;
42
this.port = port;
43
}
44
45
/**與服務器建立連接,發送GET請求命令*/
46
public void run() {
47
Socket socket;
48
BufferedInputStream in;
49
PrintWriter out;
50
try {
51
socket = new Socket(host, port);
52
System.out.println(socket.getRemoteSocketAddress());
53
System.out.println("連接成功");
54
in = new BufferedInputStream(socket.getInputStream());
55
out = new PrintWriter(socket.getOutputStream(), true);
56
try {
57
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
58
String userInput;
59
while ((userInput = stdIn.readLine()) != null) {
60
if (userInput.equalsIgnoreCase("quit")) {
61
System.out.println("關閉瀏覽器");
62
System.exit(0);
63
}
64
out.println(userInput);
65
out.println();
66
System.out.println(userInput);
67
int mark = 0;
68
int word = -1;
69
byte data[] = new byte[0];
70
byte data2[] = new byte[1000];
71
while ((word = in.read(data2, 0, 1000)) != -1) {
72
byte temp[] = data;
73
data = new byte[data.length + word];
74
System.arraycopy(temp, 0, data, 0, temp.length);
75
System.arraycopy(data2, 0, data, temp.length, word);
76
}
77
for (int i = 0; i < data.length; i++) {
78
if (data[i] == 13 && data[i + 1] == 10 && data[i + 2] == 13 && data[i + 3] == 10) {
79
mark = i + 4;
80
break;
81
}
82
}
83
byte head[] = new byte[mark];
84
System.arraycopy(data, 0, head, 0, mark);
85
System.out.println("頭文件:");
86
System.out.println(new String(head));
87
byte file[] = new byte[data.length - mark];
88
System.arraycopy(data, mark, file, 0, data.length - mark);
89
save(userInput, file);
90
//重新建立連接
91
System.out.println("重新建立連接");
92
socket = new Socket(host, port);
93
System.out.println(socket.getRemoteSocketAddress());
94
System.out.println("連接成功");
95
in = new BufferedInputStream(socket.getInputStream());
96
out = new PrintWriter(socket.getOutputStream(), true);
97
}
98
99
} catch (IOException e1) {
100
e1.printStackTrace();
101
}
102
} catch (IOException e2) {
103
System.out.println("連接失敗");
104
e2.printStackTrace();
105
System.exit(0);
106
}
107
}
108
109
/**保存get獲取的文件*/
110
void save(String userInput, byte bf[]) {
111
StringTokenizer t = new StringTokenizer(userInput, " ");
112
if (t.countTokens() < 2) {
113
return;
114
}
115
t.nextToken();
116
String t2 = t.nextToken();
117
int last = t2.lastIndexOf("/");
118
//獲取文件名
119
String filename = t2.substring(last + 1, t2.length());
120
if (filename.equals("")) {
121
filename = "index.html";
122
}
123
String dir = t2.substring(0, last);
124
//獲取文件的目錄
125
126
File dirs = new File(host + dir);
127
dirs.mkdirs();
128
File file = new File(dirs, filename);
129
try {
130
FileOutputStream out = new FileOutputStream(file);
131
out.write(bf);
132
System.out.println("文件" + filename + "成功保存到文件夾" + host + dir);
133
} catch (Exception e) {
134
// TODO Auto-generated catch block
135
System.out.println("文件保存失敗");
136
}
137
}
138
}
139
直接打印頭文件,保存GET的文件;沒有分析響應頭的代碼
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

2.簡單的每請求線程模型服務器
ThreadedServer監聽一個端口,對每個Socket請求生成一個Handler線程進行處理,服務器文件放在一個www文件夾中
1
/*
2
* To change this template, choose Tools | Templates
3
* and open the template in the editor.
4
*/
5
package http;
6
7
import java.net.ServerSocket;
8
9
/*
10
* To change this template, choose Tools | Templates
11
* and open the template in the editor.
12
*/
13
import java.net.Socket;
14
15
/**
16
*這是一個多線程簡單的HTTP服務器程序,它可以接受HTTP1.0的GET命令
17
* @author wangliang
18
*/
19
public class ThreadedServer {
20
21
String serverName;
22
String Version;
23
int serverport;
24
25
public static void main(String args[]) {
26
ThreadedServer server = new ThreadedServer("HTTPSServer", "1.0", 801);
27
server.run();
28
}
29
30
public ThreadedServer(String name, String version, int port) {
31
serverName = name;
32
Version = version;
33
serverport = port;
34
}
35
36
/**為客戶端的每個請求啟動一個線程處理用戶的請求*/
37
public void run() {
38
int count=1;
39
System.out.println(serverName + " version:" + Version);
40
try {
41
ServerSocket server = new ServerSocket(serverport);
42
do {
43
Socket client = server.accept();
44
(new Handler(client)).start();
45
System.out.println("Now " + (count++) + " requests received!");
46
} while (true);
47
} catch (Exception e) {
48
e.printStackTrace();
49
System.exit(1);
50
}
51
}
52
}
53

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





























































































posted on 2008-04-13 00:31 littlefermat 閱讀(1746) 評論(0) 編輯 收藏