Results 1 to 3 of 3
- 11-27-2008, 07:51 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 1
- Rep Power
- 0
GET html contents from a web server
hi
i am building a program to get the contents of html on an http website.
The code is found below:
//Get details of the url (RequestTrial) I would like to connect to
//The 'RequestTrial' stands for the url
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...
- 03-13-2009, 07:57 PM #2
Member
- Join Date
- Mar 2009
- Posts
- 13
- Rep Power
- 0
you can use
URLConnection con = yourURL.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
- 03-14-2009, 07:47 AM #3
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
Similar Threads
-
How can I include a html file in html textarea?
By surya_dks in forum New To JavaReplies: 2Last Post: 10-04-2008, 07:20 AM -
Viewing contents of zip file
By Java Tip in forum Java TipReplies: 0Last Post: 03-03-2008, 05:16 PM -
Concatenation file contents
By Java Tip in forum Java TipReplies: 1Last Post: 02-07-2008, 01:29 PM -
Reading web contents
By javaplus in forum NetworkingReplies: 2Last Post: 11-29-2007, 10:28 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks