Hey all -
I have to create a basic client browser that creates a socket with a webpage and then uses a command similar to :
GET index.html\r\nHTTP/1.1\r\nHost: www.mysite.com\r\n\r\n
To get the source code for the website. I will store each line recieved from the server into an arrayList, which i will then go through and count certain words within the source code.
My first problem is obviously correctly connecting to a server. My attempts so far using anything close to the above within Socket creation always ends up with a UnknownHost Exception. Am i even using this in the correct manner? Does anyone know what I am trying to achieve with that?!
For now, what i did is the following:
1) Created a connection with a socket to my localhost (i currently have WAMP installed and is running a server in the background).
2) When connected, and i have the following code:
try {
sock = new Socket("localhost", 80);
out = new PrintWriter(sock.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
kbd = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a string: ");
while (true)
{
line = kbd.readLine();
if (line.equals("."))
{
sock.close();
break;
}
out.println(line);
System.out.println(in.readLine());
}
}
catch (UnknownHostException uhe)
{
System.out.println("Unknown Host");
}
catch (IOException ioe)
{
System.out.println("IO error: " + ioe.getMessage());
}
It asks for user input, if i type anything (except a period . ) i get this output:
Enter a string:
hello
<?xml version="1.0" encoding="iso-8859-1"?>
Which is the first line of HTML from within my localhost root (index.html) file.
How would i go about taking this simple code to make it go through until EOF - storing the values into an arrayList.
My attempt was something like:
while (true)
{
line = in.readLine();
arrayList.add(line);
System.out.println(arrayList);
}
But this doesn't read/print anything (i would think it would print each time a new line was added to the arrayList).
I am either not using flush() or write() or something else. Any help would be greatly appreciated!
Thanks