Read An HttpURLConnection’s InputStream Line By Line
Posted by blogmeister on
July 22, 2009
To read an HttpURLConnection’s InputStream line by line, just like what you do reading a file using a FileReader, you can use the InputStreamReader class as parameter to the BufferedReader class and do the same code you do for reading a file line by line. Take this code for example.
|
1 2 3 4 5 6 7 8 9 |
URL url = new URL("SITE"); HttpURLConnection httpcon = (HttpURLConnection) url.openConnection(); BufferedReader br = new BufferedReader(new InputStreamReader(httpcon.getInputStream())); String line = null; while ((line = br.readLine()) != null) { System.out.println(line); } httpcon.disconnect(); br.close(); |
Display Header Fields Of HttpURLConnection In Java
Posted by blogmeister on
April 30, 2009
Here is a short code to list header fields returned after you access a url.
|
1 2 3 4 5 6 7 8 |
public void display(HttpURLConnection httpcon) { Map headerfields = httpcon.getHeaderFields(); Set headers = headerfields.entrySet(); for(Iterator i = headers.iterator(); i.hasNext();){ Map.Entry map = (Map.Entry)i.next(); System.out.println(map.getKey() + " : " + map.getValue()); } } |









