UnknownHostException
I wanna access simple Http site...
I think ter is an error in the socket obj creation... pls help
urgent...
import java.io.*;
import java.net.*;
public class http
{
public static void main(String[] args)
{
try
{
OutputStream to_file = new FileOutputStream("f:
temp.txt");
URL url = new URL("http://www.wickedlysmart.com/HeadFirst/
HeadFirstJava/HeadFirstJavaIndex.html");
String protocol = url.getProtocol();
String host = url.getHost();
int port = url.getPort();
if(port == -1) port = 80;
String filename =url.getFile();
System.out.println(filename+"\n\n");
Socket socket = new Socket(host, port);
InputStream from_server = socket.getInputStream();
PrintWriter to_server = new PrintWriter(socket.getOutputStream());
to_server.print("Get" + filename +"\n\n");
to_server.flush();
byte[] buffer = new byte[4096];
int byte_read;
while((byte_read = from_server.read(buffer)) != -1)
{
to_file.write(buffer,0,byte_read);
System.out.print((char)byte_read);
}
socket.close();
to_file.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Output:
C:\java\bin>java Http
/HeadFirst/HeadFirstJava/HeadFirstJavaIndex.html
java.net.UnknownHostException: www.wickedlysmart.com
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:364)
at java.net.Socket.connect(Socket.java:507)
at java.net.Socket.connect(Socket.java:457)
at java.net.Socket.<init>(Socket.java:365)
at java.net.Socket.<init>(Socket.java:178)
at Http.main(Http.java:20)
C:\java\bin>
the output file f:\temp.txt is created but empty...:(
pls help out from this error...