List of usage examples for java.net HttpURLConnection setFollowRedirects
public static void setFollowRedirects(boolean set)
From source file:Main.java
public static void main(String[] argv) throws Exception { HttpURLConnection.setFollowRedirects(false); HttpURLConnection con = (HttpURLConnection) new URL("http://www.google.coom").openConnection(); con.setRequestMethod("HEAD"); System.out.println(con.getResponseCode() == HttpURLConnection.HTTP_PARTIAL); }
From source file:Main.java
public static void main(String[] argv) throws Exception { HttpURLConnection.setFollowRedirects(false); HttpURLConnection con = (HttpURLConnection) new URL("http://www.google.coom").openConnection(); con.setRequestMethod("HEAD"); System.out.println(con.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP); }
From source file:Main.java
public static void main(String[] argv) throws Exception { HttpURLConnection.setFollowRedirects(false); HttpURLConnection con = (HttpURLConnection) new URL("http://www.google.coom").openConnection(); con.setRequestMethod("HEAD"); System.out.println(con.getResponseCode() == HttpURLConnection.HTTP_NOT_AUTHORITATIVE); }
From source file:Main.java
public static void main(String[] argv) throws Exception { HttpURLConnection.setFollowRedirects(false); URL url = new URL("http://hostname:80"); URLConnection conn = url.openConnection(); HttpURLConnection httpConn = (HttpURLConnection) conn; httpConn.setInstanceFollowRedirects(false); conn.connect();/*from w ww.ja va2 s . co m*/ }
From source file:Main.java
public static String getHtml(String getUrl, String charsetName) { String html = ""; URL url;/* w w w. jav a 2 s . c om*/ try { url = new URL(getUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("User-Agent", userAgent); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Cache-Control", "no-cache"); connection.setReadTimeout(timeout); connection.setFollowRedirects(true); connection.connect(); InputStream inStrm = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(inStrm, charsetName)); String temp = ""; while ((temp = br.readLine()) != null) { html = html + (temp + '\n'); } } catch (Exception e) { e.printStackTrace(); } return html; }
From source file:Main.java
static String downloadHtml(String urlString) { StringBuffer buffer = new StringBuffer(); try {/*from w w w. j a v a 2 s.c o m*/ URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); HttpURLConnection.setFollowRedirects(true); conn.setRequestProperty("Accept-Encoding", "gzip, deflate"); String encoding = conn.getContentEncoding(); InputStream inStr = null; if (encoding != null && encoding.equalsIgnoreCase("gzip")) { inStr = new GZIPInputStream(conn.getInputStream()); } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) { inStr = new InflaterInputStream(conn.getInputStream(), new Inflater(true)); } else { inStr = conn.getInputStream(); } int ptr = 0; InputStreamReader inStrReader = new InputStreamReader(inStr, Charset.forName("GB2312")); while ((ptr = inStrReader.read()) != -1) { buffer.append((char) ptr); } inStrReader.close(); conn.disconnect(); inStr.close(); } catch (Exception e) { e.printStackTrace(); } return buffer.toString(); }
From source file:Main.java
public static String getPosthtml(String posturl, String postData, String encode) { String html = ""; URL url;/* w w w. jav a 2 s .co m*/ try { url = new URL(posturl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("User-Agent", userAgent); String postDataStr = postData; byte[] bytes = postDataStr.getBytes("utf-8"); connection.setRequestProperty("Content-Length", "" + bytes.length); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Cache-Control", "no-cache"); connection.setDoOutput(true); connection.setReadTimeout(timeout); connection.setFollowRedirects(true); connection.connect(); OutputStream outStrm = connection.getOutputStream(); outStrm.write(bytes); outStrm.flush(); outStrm.close(); InputStream inStrm = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(inStrm, encode)); String temp = ""; while ((temp = br.readLine()) != null) { html += (temp + '\n'); } br.close(); connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } return html; }
From source file:com.comcast.cmb.test.tools.CNSTestingUtils.java
public static String sendHttpMessage(String endpoint, String message) throws Exception { if ((message == null) || (endpoint == null)) { throw new Exception("Message and Endpoint must both be set"); }/*from ww w .j a va 2 s . co m*/ String newPostBody = message; byte newPostBodyBytes[] = newPostBody.getBytes(); URL url = new URL(endpoint); logger.info(">> " + url.toString()); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("GET"); // POST no matter what con.setDoOutput(true); con.setDoInput(true); con.setFollowRedirects(false); con.setUseCaches(true); logger.info(">> " + "GET"); //con.setRequestProperty("content-length", newPostBody.length() + ""); con.setRequestProperty("host", url.getHost()); con.connect(); logger.info(">> " + newPostBody); int statusCode = con.getResponseCode(); BufferedInputStream responseStream; logger.info("StatusCode:" + statusCode); if (statusCode != 200 && statusCode != 201) { responseStream = new BufferedInputStream(con.getErrorStream()); } else { responseStream = new BufferedInputStream(con.getInputStream()); } int b; String response = ""; while ((b = responseStream.read()) != -1) { response += (char) b; } logger.info("response:" + response); return response; }
From source file:org.bibsonomy.scraper.url.kde.blackwell.BlackwellSynergyScraper.java
/** FIXME: refactor * Extract the content of a scitation.aip.org page. * (changed code from ScrapingContext.getContentAsString) * @param urlConn Connection to api page (from url.openConnection()) * @param cookie Cookie for auth.//from w w w . j a v a 2 s . c o m * @return Content of aip page. * @throws IOException */ private String getPageContent(HttpURLConnection urlConn, String cookie) throws IOException { urlConn.setAllowUserInteraction(true); urlConn.setDoInput(true); urlConn.setDoOutput(false); urlConn.setUseCaches(false); urlConn.setFollowRedirects(true); urlConn.setInstanceFollowRedirects(false); urlConn.setRequestProperty("Cookie", cookie); urlConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)"); urlConn.connect(); // build content StringWriter out = new StringWriter(); InputStream in = new BufferedInputStream(urlConn.getInputStream()); int b; while ((b = in.read()) >= 0) { out.write(b); } urlConn.disconnect(); in.close(); out.flush(); out.close(); return out.toString(); }
From source file:org.bibsonomy.scraper.url.kde.blackwell.BlackwellSynergyScraper.java
/** FIXME: refactor * Gets the cookie which is needed to extract the content of aip pages. * (changed code from ScrapingContext.getContentAsString) * @param urlConn Connection to api page (from url.openConnection()) * @return The value of the cookie.// w w w. ja v a 2 s . c om * @throws IOException */ private String getCookie() throws IOException { HttpURLConnection urlConn = (HttpURLConnection) new URL("http://www.blackwell-synergy.com/help") .openConnection(); String cookie = null; urlConn.setAllowUserInteraction(true); urlConn.setDoInput(true); urlConn.setDoOutput(false); urlConn.setUseCaches(false); urlConn.setFollowRedirects(true); urlConn.setInstanceFollowRedirects(false); urlConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)"); urlConn.connect(); // extract cookie from header Map map = urlConn.getHeaderFields(); cookie = urlConn.getHeaderField("Set-Cookie"); if (cookie != null && cookie.indexOf(";") >= 0) cookie = cookie.substring(0, cookie.indexOf(";")); urlConn.disconnect(); return cookie; }