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 FileOutputStrea m("f:
temp.txt");
URL url = new URL("http://www.wickedlysma rt.com/HeadFirst/
HeadFirstJava/HeadFirstJavaIn dex.html");
String protocol = url.getProtocol ();
String host = url.getHost();
int port = url.getPort();
if(port == -1) port = 80;
String filename =url.getFile();
System.out.prin tln(filename+"\ n\n");
Socket socket = new Socket(host, port);
InputStream from_server = socket.getInput Stream();
PrintWriter to_server = new PrintWriter(soc ket.getOutputSt ream());
to_server.print ("Get" + filename +"\n\n");
to_server.flush ();
byte[] buffer = new byte[4096];
int byte_read;
while((byte_rea d = from_server.rea d(buffer)) != -1)
{
to_file.write(b uffer,0,byte_re ad);
System.out.prin t((char)byte_re ad);
}
socket.close();
to_file.close() ;
}
catch(Exception e)
{
e.printStackTra ce();
}
}
}
Output:
C:\java\bin>jav a Http
/HeadFirst/HeadFirstJava/HeadFirstJavaIn dex.html
java.net.Unknow nHostException: www.wickedlysma rt.com
at java.net.PlainS ocketImpl.conne ct(PlainSocketI mpl.java:177)
at java.net.SocksS ocketImpl.conne ct(SocksSocketI mpl.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...
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 FileOutputStrea m("f:
temp.txt");
URL url = new URL("http://www.wickedlysma rt.com/HeadFirst/
HeadFirstJava/HeadFirstJavaIn dex.html");
String protocol = url.getProtocol ();
String host = url.getHost();
int port = url.getPort();
if(port == -1) port = 80;
String filename =url.getFile();
System.out.prin tln(filename+"\ n\n");
Socket socket = new Socket(host, port);
InputStream from_server = socket.getInput Stream();
PrintWriter to_server = new PrintWriter(soc ket.getOutputSt ream());
to_server.print ("Get" + filename +"\n\n");
to_server.flush ();
byte[] buffer = new byte[4096];
int byte_read;
while((byte_rea d = from_server.rea d(buffer)) != -1)
{
to_file.write(b uffer,0,byte_re ad);
System.out.prin t((char)byte_re ad);
}
socket.close();
to_file.close() ;
}
catch(Exception e)
{
e.printStackTra ce();
}
}
}
Output:
C:\java\bin>jav a Http
/HeadFirst/HeadFirstJava/HeadFirstJavaIn dex.html
java.net.Unknow nHostException: www.wickedlysma rt.com
at java.net.PlainS ocketImpl.conne ct(PlainSocketI mpl.java:177)
at java.net.SocksS ocketImpl.conne ct(SocksSocketI mpl.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...
Comment