69 |
* @author Aaron M. Renn <arenn@urbanophile.com> |
* @author Aaron M. Renn <arenn@urbanophile.com> |
70 |
* @author Warren Levy <warrenl@cygnus.com> |
* @author Warren Levy <warrenl@cygnus.com> |
71 |
*/ |
*/ |
72 |
public class Connection extends HttpURLConnection |
public final class Connection extends HttpURLConnection |
73 |
{ |
{ |
74 |
/** |
/** |
75 |
* The socket we are connected to |
* The socket we are connected to |
124 |
socket = new Socket(url.getHost(), 80); |
socket = new Socket(url.getHost(), 80); |
125 |
else |
else |
126 |
socket = new Socket(url.getHost(), url.getPort()); |
socket = new Socket(url.getHost(), url.getPort()); |
127 |
|
|
128 |
|
if (doInput) |
129 |
|
inputStream |
130 |
|
= new DataInputStream (new BufferedInputStream (socket.getInputStream())); |
131 |
|
|
132 |
outputStream = new BufferedOutputStream(socket.getOutputStream()); |
if (doOutput) |
133 |
outputWriter = new PrintWriter(new OutputStreamWriter(outputStream, "8859_1")); |
outputStream = new BufferedOutputStream (socket.getOutputStream()); |
134 |
|
|
135 |
|
bufferedOutputStream = new ByteArrayOutputStream (256); //default is too small |
136 |
|
outputWriter = new PrintWriter (new OutputStreamWriter (outputStream, "8859_1")); |
137 |
connected = true; |
connected = true; |
138 |
|
|
139 |
|
sendRequest(); |
140 |
|
receiveReply(); |
141 |
|
} |
142 |
|
|
143 |
|
/** |
144 |
|
* Disconnects from the remote server. |
145 |
|
*/ |
146 |
|
public void disconnect() |
147 |
|
{ |
148 |
|
if (socket != null) |
149 |
|
{ |
150 |
|
try |
151 |
|
{ |
152 |
|
socket.close(); |
153 |
|
} |
154 |
|
catch (IOException e) |
155 |
|
{ |
156 |
|
// Ignore errors in closing socket. |
157 |
|
} |
158 |
|
|
159 |
|
socket = null; |
160 |
|
} |
161 |
} |
} |
162 |
|
|
163 |
/** |
/** |
164 |
* Write HTTP request header and content to outputWriter. |
* Write HTTP request header and content to outputWriter. |
165 |
*/ |
*/ |
166 |
void SendRequest() throws IOException |
void sendRequest() throws IOException |
167 |
{ |
{ |
168 |
// Send request including any request properties that were set. |
// Send request including any request properties that were set. |
169 |
outputWriter.print (getRequestMethod() + " " + url.getFile() |
outputWriter.print (getRequestMethod() + " " + url.getFile() |
196 |
setRequestProperty ("Content-type", "application/x-www-form-urlencoded"); |
setRequestProperty ("Content-type", "application/x-www-form-urlencoded"); |
197 |
} |
} |
198 |
|
|
199 |
|
// Set correct content length. |
200 |
|
setRequestProperty ("Content-length", String.valueOf (bufferedOutputStream.size())); |
201 |
|
|
202 |
// Write all req_props name-value pairs to the output writer. |
// Write all req_props name-value pairs to the output writer. |
203 |
Iterator itr = getRequestProperties().entrySet().iterator(); |
Iterator itr = getRequestProperties().entrySet().iterator(); |
204 |
|
|
208 |
outputWriter.print (e.getKey() + ": " + e.getValue() + "\r\n"); |
outputWriter.print (e.getKey() + ": " + e.getValue() + "\r\n"); |
209 |
} |
} |
210 |
|
|
|
// Write Content-type and length |
|
|
if (bufferedOutputStream != null) |
|
|
{ |
|
|
outputWriter.print ("Content-type: application/x-www-form-urlencoded\r\n"); |
|
|
outputWriter.print ("Content-length: " |
|
|
+ String.valueOf (bufferedOutputStream.size()) + "\r\n"); |
|
|
} |
|
|
|
|
211 |
// One more CR-LF indicates end of header. |
// One more CR-LF indicates end of header. |
212 |
outputWriter.print ("\r\n"); |
outputWriter.print ("\r\n"); |
213 |
outputWriter.flush(); |
outputWriter.flush(); |
214 |
|
|
215 |
// Write content |
// Write content |
216 |
if (bufferedOutputStream != null) |
bufferedOutputStream.writeTo (outputStream); |
217 |
{ |
outputStream.flush(); |
|
bufferedOutputStream.writeTo (outputStream); |
|
|
outputStream.flush(); |
|
|
} |
|
218 |
} |
} |
219 |
|
|
220 |
/** |
/** |
221 |
* Read HTTP reply from inputStream. |
* Read HTTP reply from inputStream. |
222 |
*/ |
*/ |
223 |
void ReceiveReply() throws IOException |
void receiveReply() throws IOException |
224 |
{ |
{ |
225 |
// Parse the reply |
// Parse the reply |
226 |
String line = inputStream.readLine(); |
String line = inputStream.readLine(); |
248 |
// Now read the header lines |
// Now read the header lines |
249 |
String key = null, value = null; |
String key = null, value = null; |
250 |
|
|
251 |
for (;;) |
while (true) |
252 |
{ |
{ |
253 |
line = inputStream.readLine(); |
line = inputStream.readLine(); |
254 |
|
|
312 |
} |
} |
313 |
|
|
314 |
/** |
/** |
|
* Disconnects from the remote server |
|
|
*/ |
|
|
public void disconnect() |
|
|
{ |
|
|
try |
|
|
{ |
|
|
if (socket != null) |
|
|
socket.close(); |
|
|
} |
|
|
catch (IOException e) |
|
|
{ |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
315 |
* Overrides java.net.HttpURLConnection.setRequestMethod() in order to |
* Overrides java.net.HttpURLConnection.setRequestMethod() in order to |
316 |
* restrict the available methods to only those we support. |
* restrict the available methods to only those we support. |
317 |
* |
* |
388 |
if (!connected) |
if (!connected) |
389 |
connect(); |
connect(); |
390 |
|
|
|
inputStream |
|
|
= new DataInputStream (new BufferedInputStream (socket.getInputStream())); |
|
|
|
|
|
SendRequest(); |
|
|
ReceiveReply(); |
|
|
|
|
391 |
return inputStream; |
return inputStream; |
392 |
} |
} |
393 |
|
|
403 |
if (!connected) |
if (!connected) |
404 |
connect(); |
connect(); |
405 |
|
|
|
if(bufferedOutputStream == null) |
|
|
bufferedOutputStream = new ByteArrayOutputStream (256); //default is too small |
|
|
|
|
406 |
return bufferedOutputStream; |
return bufferedOutputStream; |
407 |
} |
} |
408 |
|
|