Archived

This topic is now archived and is closed to further replies.

[java] Read file from webpage

This topic is 5398 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here is a class I made along time ago for a chat applet. It was intended to work with an ASP or CFML page to get the text off of it. Basically all this thing does is connect to a page, and get the page data. It doesn''t parse the text. That is up to you.


  
import java.io.*;
import java.net.*;

public class connector
{

private DataInputStream DIS;
private URL tmpUrl;
private String strProtocol, strHost, strFile;
private int intPort;

connector(String strURL){
try{tmpUrl = new URL(strURL);}
catch(MalformedURLException e){
System.out.println("URL Error: Doesn''t seem to be a valid URL");
}
try{
DIS = new DataInputStream(tmpUrl.openConnection().getInputStream());
}
catch(IOException e){
System.out.println("I/O Error getting in stream data");
}
}

connector(String sProt, String sHost, String sFile, int iPort){
try{tmpUrl = new URL(sProt, sHost, iPort, sFile);}
catch(MalformedURLException e){
System.out.println("URL Error: Doesn''t seem to be a valid URL");
}
try{
DIS = new DataInputStream(tmpUrl.openConnection().getInputStream());
}
catch(IOException e){
System.out.println("I/O Error getting in stream data");
}
}

public String getData()
{
boolean readMore = true;
String tmpStr = null;
StringBuffer fullStr = new StringBuffer();
try{
while(readMore){
tmpStr = DIS.readLine();
if(tmpStr == null){readMore = false;}
else{fullStr.append(tmpStr);}
}
}
catch(IOException e){
System.out.println("I/O error assigning string data.");
}
return fullStr.toString();
}

public void connect(String strURL){
try{tmpUrl = new URL(strURL);}
catch(MalformedURLException e){
System.out.println("URL Error: Doesn''t seem to be a valid URL");
}
try{
DIS = new DataInputStream(tmpUrl.openConnection().getInputStream());
}
catch(IOException e){
System.out.println("I/O Error getting in stream data");
}
}

public void connect(String sProt, String sHost, String sFile, int iPort)
{
try{tmpUrl = new URL(sProt, sHost, iPort, sFile);}
catch(MalformedURLException e){
System.out.println("URL Error: Doesn''t seem to be a valid URL");
}
try{
DIS = new DataInputStream(tmpUrl.openConnection().getInputStream());
}
catch(IOException e){
System.out.println("I/O Error getting in stream data");
}
}

}

Share this post


Link to post
Share on other sites
Wrathnut is correct, but you also have to remenber that browsers have security restrictions, and those are a pain in the ***.



----------------------------
^_^

Share this post


Link to post
Share on other sites