Skip to content

Instantly share code, notes, and snippets.

@biswarupadhikari
Created July 22, 2012 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biswarupadhikari/3160571 to your computer and use it in GitHub Desktop.
Save biswarupadhikari/3160571 to your computer and use it in GitHub Desktop.
How to get URL content using Java
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
public class API {
private String webPageUrl="";
public API(String URL){
this.webPageUrl=URL;
}
public String getContent(){
StringBuilder sb=new StringBuilder();
try {
URL url=new URL(this.webPageUrl);
InputStream stream=url.openStream();
BufferedInputStream bf=new BufferedInputStream(stream);
while (true) {
int data=bf.read();
if(data==-1){
break;
}else{
sb.append((char)data);
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}
public class Test {
public static void main(String[] args) {
API API=new API("http://localhost/api/api.php");
String MyWebPageData=API.getContent();
System.out.print(MyWebPageData);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment