Created
June 30, 2018 04:55
-
-
Save eMahtab/ada4d06714b6c7ad1d9e451577b5100d to your computer and use it in GitHub Desktop.
Reading a web page content
This file contains hidden or 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
package net.mahtabalam; | |
import java.io.BufferedInputStream; | |
import java.io.BufferedOutputStream; | |
import java.io.FileOutputStream; | |
import java.io.InputStream; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.util.List; | |
import java.util.Map; | |
public class Test { | |
public static void main(String[] args) throws Exception { | |
URL url = new URL("http://mahtabalam.net:80"); | |
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); | |
httpCon.connect(); | |
System.out.println("Content Type "+httpCon.getContentType()); | |
System.out.println("Response Code "+httpCon.getResponseCode()); | |
Map<String, List<String>> map = httpCon.getHeaderFields(); | |
for (String key : map.keySet()) { | |
System.out.println(key + ":"); | |
List<String> values = map.get(key); | |
for (String aValue : values) { | |
System.out.println("\t" + aValue); | |
} | |
} | |
InputStream inputStream = httpCon.getInputStream(); | |
BufferedInputStream reader = new BufferedInputStream(inputStream); | |
BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream("/tmp/page.html")); | |
byte[] buffer = new byte[4096]; | |
int bytesRead = -1; | |
while ((bytesRead = reader.read(buffer)) != -1) { | |
writer.write(buffer, 0, bytesRead); | |
} | |
writer.close(); | |
reader.close(); | |
System.out.println("----------Read all content---------"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment