Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

23
LINES

< > BotCompany Repo | #2000480 // loadPage

New Tinybrain snippet

1  
  public static String loadPage(URL url) throws IOException {
2  
    System.out.println("Loading: " + url.toExternalForm());
3  
    URLConnection con = url.openConnection();
4  
    return loadPage(con, url);
5  
  }
6  
7  
  public static String loadPage(URLConnection con, URL url) throws IOException {
8  
    String contentType = con.getContentType();
9  
    if (contentType == null)
10  
      throw new IOException("Page could not be read: " + url);
11  
    //Log.info("Content-Type: " + contentType);
12  
    String charset = guessCharset(contentType);
13  
    Reader r = new InputStreamReader(con.getInputStream(), charset);
14  
    StringBuilder buf = new StringBuilder();
15  
    while (true) {
16  
      int ch = r.read();
17  
      if (ch < 0)
18  
        break;
19  
      //Log.info("Chars read: " + buf.length());
20  
      buf.append((char) ch);
21  
    }
22  
    return buf.toString();
23  
  }

download  show line numbers   

Snippet is not live.

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #2000480
Snippet name: loadPage
Eternal ID of this version: #2000480/1
Text MD5: b63eefcd2865833ddc16fa3a4af1a5b3
Author: stefan
Category:
Type: New Tinybrain snippet
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-02 14:49:28
Source code size: 849 bytes / 23 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 542 / 142
Referenced in: [show references]