Java network & proxy server

K

King W.Wang

Hi all,
I've copied the following program from a book and tried to run it.
But I received the Exception:

UnknownHostException: java.net.UnknownHostException: java.sun.com

I work in a network with firewall and proxy server. Each time when
I try to open a URL, I'm prompted to input my username and password
for the proxy server. I guess that the problem lies in the proxy
server. My question is, how should the program be modified so that
the program will work? If it is too complex, can someone give me
a reference point to an example program? Many thanks!

king

// the program:
import java.io.*;
import java.net.*;
/**
* An application that opens a connection to a web server and reads
* a single Web page from the connection.
*/
public class SimpleWebClient {
public static void main(String args[]) {
try {
// Open a client socket connection
Socket clientSocket1 = new Socket("java.sun.com", 80);
System.out.println("Client1: " + clientSocket1);
// Get a Web page
getPage(clientSocket1);
} catch (UnknownHostException uhe) {
System.out.println("UnknownHostException: " + uhe);
} catch (IOException ioe) {
System.err.println("IOException: " + ioe);
}
}
/**
* Request a Web page using the passed client socket.
* Display the reply and close the client socket.
*/
public static void getPage(Socket clientSocket) {
try {
// Acquire the input and output streams
DataOutputStream outbound = new DataOutputStream(
clientSocket.getOutputStream() );
BufferedReader inbound = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()) );
// Write the HTTP request to the server
outbound.writeBytes("GET / HTTP/1.0\r\n\r\n");
// Read the response
String responseLine;
while ((responseLine = inbound.readLine()) != null) {
// Display each line to the console
System.out.println(responseLine);
}
// Clean up
outbound.close();
inbound.close();
clientSocket.close();
} catch (IOException ioe) {
System.out.println("IOException: " + ioe);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top