hi
i am building a program to get the contents of html on an http website.
The code is found below:
| Code: |
//Get details of the url (RequestTrial) I would like to connect to
//The 'RequestTrial' stands for the url, example, www.yahoo.com
url = new URL(RequestTrial);
String getHost = url.getHost();
int getPort = url.getPort();
if (getPort == -1) {
getPort = 80;
}
SrvrSocket = new Socket(getHost, getPort);
System.out.println("connected to the server requested");
//Now i send the GET request to the server
PrintWriter wrServer = new PrintWriter(new OutputStreamWriter(SrvrSocket.getOutputStream()));
wrServer.println("GET " + RequestTrial + " HTTP/1.0" + "\r\n");
wrServer.println(""); // followed by newline
wrServer.flush();
//Receiving the data from the server
// Check reply headers
DataInputStream Din =
new DataInputStream(SrvrSocket.getInputStream());
String str = Din.readLine();
HTMLstring += str;
System.out.println(str);
String tempStr = new String(str + "\r\n");
if (str.length() > 0) {
while (true) {
str = Din.readLine();
tempStr = new String(str + "\r\n");
//HTMLstring += tempStr;
System.out.println(tempStr);
if (str.length() <= 0) {
break;
}
}
}
InputStream rdServer = SrvrSocket.getInputStream();
InputStreamReader isReader = new InputStreamReader(rdServer);
BufferedReader rd = new BufferedReader(isReader);
//HTMLstring is where i am holding the HTML given by the server
String s = null;
while ((s = rd.readLine()) != null) {
HTMLstring += s + "\r\n";
}
rd.close();
} catch (UnknownHostException uhe) {
// Requested Server could not be located
System.out.println("Server Not Found: " + uhe);
} catch (IOException ioe) {
System.out.println("IOException: " + ioe);
} finally {
try {
SrvrSocket.close();
} catch (Exception e) {
System.out.println("Exception: " + e);
}
} |
But unfortunately i only receive half of the html
why is that???
When i use the URL class getcontent I get all the html, but i need to use sockets. Can someone please indicate where my error is please...