-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathURLConnectionTest.java
More file actions
168 lines (149 loc) · 5.01 KB
/
Copy pathURLConnectionTest.java
File metadata and controls
168 lines (149 loc) · 5.01 KB
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
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
package urlConnection;
import java.io.*;
import java.net.*;
import java.util.*;
/**
* This program connects to an URL and displays the response header data and the first 10 lines of
* the requested data.
*
* Supply the URL and an optional username and password (for HTTP basic authentication) on the
* command line.
* @version 1.11 2007-06-26
* @author Cay Horstmann
*/
public class URLConnectionTest
{
public static void main(String[] args)
{
try
{
String urlName;
if (args.length > 0) urlName = args[0];
else urlName = "http://horstmann.com";
URL url = new URL(urlName);
URLConnection connection = url.openConnection();
// set username, password if specified on command line
if (args.length > 2)
{
String username = args[1];
String password = args[2];
String input = username + ":" + password;
String encoding = base64Encode(input);
connection.setRequestProperty("Authorization", "Basic " + encoding);
}
connection.connect();
// print header fields
Map<String, List<String>> headers = connection.getHeaderFields();
for (Map.Entry<String, List<String>> entry : headers.entrySet())
{
String key = entry.getKey();
for (String value : entry.getValue())
System.out.println(key + ": " + value);
}
// print convenience functions
System.out.println("----------");
System.out.println("getContentType: " + connection.getContentType());
System.out.println("getContentLength: " + connection.getContentLength());
System.out.println("getContentEncoding: " + connection.getContentEncoding());
System.out.println("getDate: " + connection.getDate());
System.out.println("getExpiration: " + connection.getExpiration());
System.out.println("getLastModifed: " + connection.getLastModified());
System.out.println("----------");
Scanner in = new Scanner(connection.getInputStream());
// print first ten lines of contents
for (int n = 1; in.hasNextLine() && n <= 10; n++)
System.out.println(in.nextLine());
if (in.hasNextLine()) System.out.println(". . .");
}
catch (IOException e)
{
e.printStackTrace();
}
}
/**
* Computes the Base64 encoding of a string.
* @param s a string
* @return the Base64 encoding of s
*/
public static String base64Encode(String s)
{
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
Base64OutputStream out = new Base64OutputStream(bOut);
try
{
out.write(s.getBytes());
out.flush();
}
catch (IOException e)
{
}
return bOut.toString();
}
}
/**
* This stream filter converts a stream of bytes to their Base64 encoding.
*
* Base64 encoding encodes 3 bytes into 4 characters. |11111122|22223333|33444444| Each set of 6
* bits is encoded according to the toBase64 map. If the number of input bytes is not a multiple of
* 3, then the last group of 4 characters is padded with one or two = signs. Each output line is at
* most 76 characters.
*/
class Base64OutputStream extends FilterOutputStream
{
private static char[] toBase64 = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
private int col = 0;
private int i = 0;
private int[] inbuf = new int[3];
/**
* Constructs the stream filter.
* @param out the stream to filter
*/
public Base64OutputStream(OutputStream out)
{
super(out);
}
public void write(int c) throws IOException
{
inbuf[i] = c;
i++;
if (i == 3)
{
if (col >= 76)
{
super.write('\n');
col = 0;
}
super.write(toBase64[(inbuf[0] & 0xFC) >> 2]);
super.write(toBase64[((inbuf[0] & 0x03) << 4) | ((inbuf[1] & 0xF0) >> 4)]);
super.write(toBase64[((inbuf[1] & 0x0F) << 2) | ((inbuf[2] & 0xC0) >> 6)]);
super.write(toBase64[inbuf[2] & 0x3F]);
col += 4;
i = 0;
}
}
public void flush() throws IOException
{
if (i > 0 && col >= 76)
{
super.write('\n');
col = 0;
}
if (i == 1)
{
super.write(toBase64[(inbuf[0] & 0xFC) >> 2]);
super.write(toBase64[(inbuf[0] & 0x03) << 4]);
super.write('=');
super.write('=');
}
else if (i == 2)
{
super.write(toBase64[(inbuf[0] & 0xFC) >> 2]);
super.write(toBase64[((inbuf[0] & 0x03) << 4) | ((inbuf[1] & 0xF0) >> 4)]);
super.write(toBase64[(inbuf[1] & 0x0F) << 2]);
super.write('=');
}
}
}