Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
56
Chapter 2 First Servlets
Listing 2.13 HttpClient.java
import java.awt.*;
import java.net.*;
import java.io.*;
/**
* The underlying network client used by WebClient.
*/
public class HttpClient extends NetworkClient {
private String requestLine;
private String[] requestHeaders;
private TextArea outputArea;
private Interruptible app;
public HttpClient(String host, int port,
String requestLine, String[] requestHeaders,
TextArea outputArea, Interruptible app) {
super(host, port);
this.requestLine = requestLine;
this.requestHeaders = requestHeaders;
this.outputArea = outputArea;
this.app = app;
if (checkHost(host))
connect();
}
protected void handleConnection(Socket uriSocket)
throws IOException {
try {
PrintWriter out = SocketUtil.getWriter(uriSocket);
BufferedReader in = SocketUtil.getReader(uriSocket);
outputArea.setText("");
out.println(requestLine);
for(int i=0; i
if (requestHeaders[i] == null)
break;
else
out.println(requestHeaders[i]);
}
out.println();
String line;
while ((line = in.readLine()) != null &&
!app.isInterrupted())
outputArea.append(line + "\n");
if (app.isInterrupted())
outputArea.append(" Download Interrupted ");
} catch(Exception e) {
outputArea.setText("Error: " + e);
}
}
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved