日期:2014-05-17 浏览次数:20664 次
private byte[] queryData() throws Exception { java.net.URL connUrl = new URL(url); java.net.HttpURLConnection conn = (HttpURLConnection) connUrl.openConnection(); conn.setRequestProperty("User-agent","Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; Maxthon 2.0)"); java.io.InputStream input = conn.getInputStream(); byte[] data = new byte[1024]; int length = 0; ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((length = input.read(data)) > 0) { baos.write(data, 0, length); } conn.disconnect(); return baos.toByteArray(); }
java.io.InputStream input = conn.getInputStream();
String cookie = ""; do { HttpURLConnection conn = (HttpURLConnection) new URL("http://www.google.com.hk/search?q=%E5%A6%87%E5%A5%B3&hl=zh-CN").openConnection(); if(cookie.length() != 0) conn.setRequestProperty("Cookie", cookie); conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0)"); conn.setInstanceFollowRedirects(false); int code = conn.getResponseCode(); if(code == HttpURLConnection.HTTP_MOVED_TEMP) { cookie += conn.getHeaderField("Set-Cookie") + ";"; } if(conn.getResponseCode() == HttpURLConnection.HTTP_OK) break; } while(true);