Created
July 22, 2012 18:19
-
-
Save biswarupadhikari/3160571 to your computer and use it in GitHub Desktop.
How to get URL content using Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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