SOCKET基礎 學習筆記
1
package com;
2
3
import java.io.BufferedReader;
4
import java.io.ByteArrayOutputStream;
5
import java.io.FilterOutputStream;
6
import java.io.IOException;
7
import java.io.InputStreamReader;
8
import java.io.OutputStream;
9
import java.net.URL;
10
import java.net.URLConnection;
11
import java.util.Date;
12
import java.util.List;
13
import java.util.Map;
14
15
public class URLConnectionTest {
16
17
public static void main(String[] args) {
18
19
try {
20
URL url=new URL("http://www.sina.com");
21
URLConnection con=url.openConnection();
22
con.connect();
23
//批量打印出請求頭信息
24
Map<String,List<String>> map=con.getHeaderFields();
25
for(Map.Entry<String, List<String>> entry:map.entrySet()) {
26
String key=entry.getKey();
27
for(String value:entry.getValue()) {
28
System.out.println(key+"-------------"+value);
29
}
30
}
31
32
33
URLConnectionTest test=new URLConnectionTest();
34
//對字符串"chenxiaoyu"進行 Base64 編碼
35
System.out.println(test.base64Encode("chenxiaoyu"));
36
37
38
//按條件打印出相關的請求頭信息
39
System.out.println("------------------");
40
System.out.println("ContentType():"+con.getContentType());
41
System.out.println("getContentLength():"+con.getContentLength());
42
System.out.println("getContentEncoding():"+con.getContentEncoding());
43
System.out.println("getDate():"+con.getDate());
44
System.out.println("getLastModified():"+new Date(con.getLastModified()));
45
System.out.println("------------------");
46
47
//inputStreamReader 是字節到字符的一個橋梁,可以制定字符的字符編碼
48
//下面的方法也是解決網絡編程中中文亂碼常用的方法
49
//例如:已知某個response的編碼為utf-8
50
//則要正確顯示返回的信息,需要把下面的 gbk 參數換為 utf-8 ,否則會出現亂碼或者無法編碼的問題
51
BufferedReader br=new BufferedReader(new InputStreamReader(con.getInputStream(),"gbk"));
52
53
String s;
54
//下面的方法打印出新浪網首頁的信息
55
while((s=br.readLine())!=null) {
56
System.out.println(s);
57
}
58
59
60
} catch(Exception e) {
61
e.printStackTrace();
62
}
63
64
}
65
66
public String base64Encode(String s){
67
OutputStream bao=new ByteArrayOutputStream();
68
Base64OutputStream b64out=new Base64OutputStream(bao);
69
try {
70
b64out.write(s.getBytes());
71
b64out.flush();
72
} catch(Exception e) {
73
e.printStackTrace();
74
}
75
return bao.toString();
76
}
77
78
}
79
80
//下面是一個Base64編碼的實現方式,所謂Base64編碼是一種加密方式,旨在使人們一眼不能看出信息的真是內容,并不能防止被破解
81
//采用的是對每24個bit(3個字節)進行編碼,方法是按每6個bit位編碼成一個字節 則3個字節的真實數據加密后形成4個字節的數據
82
//(在高兩位補0) 每個字節的10進制數據對應固定數組 enconding 的下標 ,如果一個新字節的編碼為i則其編碼字符為enconding[i]
83
class Base64OutputStream extends FilterOutputStream {
84
85
public Base64OutputStream(OutputStream os) {
86
super(os);
87
}
88
89
public void write(int c) throws IOException {
90
buffer[i]=c;
91
i++;
92
if(i==3) {
93
//取第一個字節的前6位又移兩位得到一個新的字節
94
super.write(toBase64[buffer[0]&0xfc>>2]);
95
//取第一個字節的后兩位左移4位得到第二個新的字節的前兩位 然后取第二個字節的前四位右移4位得到第二個新字節的后四位
96
//然后將兩次取得的bit位用'|'連接起來 得到第二個新的字節
97
super.write(toBase64[((buffer[0]&0x03)<<4)|((buffer[1])&0xf0>>4)]);
98
99
super.write(toBase64[((buffer[1]&0x0f)<<2)|((buffer[2]&0xc0)>>6)]);
100
super.write(toBase64[(buffer[2]&0x3f)]);
101
col+=4;
102
i=0;
103
if(col>=76) {
104
super.write('\n');
105
col=0;
106
}
107
108
}
109
110
}
111
112
//如果待編碼的字符串不足3個字節,則采用如下方式進行編碼
113
public void flush() throws IOException {
114
//如果只有一個字節
115
if(i==1) {
116
//第一個新的字節的產生同前
117
super.write(toBase64[buffer[0]&0xfc>>2]); //base64編碼
118
//第二個新字節 取第二個字節的后兩位左移4位得到第二個新的字節,由于后面沒有待編碼的字節,所以在低四補0000
119
//第二個新的字節的二進制形式為 00XX 0000
120
super.write(toBase64[(buffer[0]&0x03)<<4]);
121
//后兩個新字節采用連個'='進行補充
122
super.write('=');
123
super.write('=');
124
}
125
//如果只有兩個字節
126
else if (i==2) {
127
//第一、二個新字節的產生
128
super.write(toBase64[buffer[0]&0xfc]>>2);
129
super.write(toBase64[((buffer[0]&0x03)<<4)|((buffer[1])&0xf0>>4)]);
130
super.write(toBase64[((buffer[1]&0x0f)<<2)]);
131
super.write('=');
132
}
133
}
134
135
136
137
private static char[] toBase64=
138
{
139
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
140
'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
141
'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
142
'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
143
};
144
145
private int i=0;
146
147
private int col=0;
148
149
private int[] buffer=new int[3];
150
151
152
153
}
154
155
156
157
158
159
160
161

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

posted on 2008-02-22 16:26 曉宇 閱讀(245) 評論(0) 編輯 收藏 所屬分類: JAVA基礎