private boolean handshake() throws Exception { URL homePage = new URL("http://mangaonweb.com/viewer.do?ctsn=" + ctsn); HttpURLConnection urlConn = (HttpURLConnection) homePage.openConnection(); urlConn.connect(); if (urlConn.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) return (false); // save the cookie String headerName = null; for (int i = 1; (headerName = urlConn.getHeaderFieldKey(i)) != null; i++) { if (headerName.equals("Set-Cookie")) { cookies = urlConn.getHeaderField(i); } } // save cdn and crcod String page = "", line; BufferedReader stream = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), "UTF-8")); while ((line = stream.readLine()) != null) page += line; cdn = param(page, "cdn"); crcod = param(page, "crcod"); return (true); }
public ArrayList<String> parseXML() throws Exception { ArrayList<String> ret = new ArrayList<String>(); handshake(); URL url = new URL( "http://mangaonweb.com/page.do?cdn=" + cdn + "&cpn=book.xml&crcod=" + crcod + "&rid=" + (int) (Math.random() * 10000)); String page = DownloaderUtils.getPage(url.toString(), "UTF-8", cookies); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(page)); Document d = builder.parse(is); Element doc = d.getDocumentElement(); NodeList pages = doc.getElementsByTagName("page"); total = pages.getLength(); for (int i = 0; i < pages.getLength(); i++) { Element e = (Element) pages.item(i); ret.add(e.getAttribute("path")); } return (ret); }
private byte[] downloadByteArray(URL url) throws IOException { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestProperty("Cookie", cookies); if (conn.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) return (null); InputStream in = conn.getInputStream(); byte[] buf = new byte[conn.getContentLength()]; int read, offset = 0; while ((read = in.read(buf, offset, buf.length - offset)) != -1) { offset += read; } return (buf); }