List of usage examples for java.net HttpURLConnection HTTP_MOVED_PERM
int HTTP_MOVED_PERM
To view the source code for java.net HttpURLConnection HTTP_MOVED_PERM.
Click Source Link
From source file:com.ikanow.infinit.e.data_model.driver.InfiniteDriver.java
private String sendPostRequest(String urlAddress, String data, int redirects) throws MalformedURLException, IOException { String result = ""; if (urlAddress.startsWith("https:")) { TrustManagerManipulator.allowAllSSL(); }/*from w w w . j a v a2s.c o m*/ URLConnection urlConnection = new URL(urlAddress).openConnection(); if (cookie != null) urlConnection.setRequestProperty("Cookie", cookie); if (apiKey != null) urlConnection.setRequestProperty("Cookie", apiKey); urlConnection.setDoOutput(true); urlConnection.setRequestProperty("Accept-Charset", "UTF-8"); ((HttpURLConnection) urlConnection).setRequestMethod("POST"); // Post JSON string to URL OutputStream os = urlConnection.getOutputStream(); byte[] b = data.getBytes("UTF-8"); os.write(b); int status = ((HttpURLConnection) urlConnection).getResponseCode(); // normally, 3xx is redirect if (status != HttpURLConnection.HTTP_OK) { if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER) { if (redirects <= 5) { String newUrlAddress = ((HttpURLConnection) urlConnection).getHeaderField("Location"); if (null != newUrlAddress) { return sendPostRequest(newUrlAddress, data, redirects + 1); } } //(else carry on, will exception out or something below) } } //TESTED // Receive results back from API InputStream inStream = urlConnection.getInputStream(); result = IOUtils.toString(inStream, "UTF-8"); inStream.close(); //save cookie if cookie is null if (cookie == null) { String headername; for (int i = 1; (headername = urlConnection.getHeaderFieldKey(i)) != null; i++) { if (headername.equals("Set-Cookie")) { cookie = urlConnection.getHeaderField(i); break; } } } return result; }
From source file:com.ikanow.infinit.e.data_model.driver.InfiniteDriver.java
public String sendGetRequest(String urlAddress, int redirects) throws Exception { if (urlAddress.startsWith("https:")) { TrustManagerManipulator.allowAllSSL(); }/*from ww w . j a v a2 s.co m*/ URL url = new URL(urlAddress); URLConnection urlConnection = url.openConnection(); if (cookie != null) urlConnection.setRequestProperty("Cookie", cookie); if (apiKey != null) urlConnection.setRequestProperty("Cookie", apiKey); ((HttpURLConnection) urlConnection).setRequestMethod("GET"); int status = ((HttpURLConnection) urlConnection).getResponseCode(); // normally, 3xx is redirect if (status != HttpURLConnection.HTTP_OK) { if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER) { if (redirects <= 5) { String newUrlAddress = ((HttpURLConnection) urlConnection).getHeaderField("Location"); if (null != newUrlAddress) { return sendGetRequest(newUrlAddress, redirects + 1); } } //(else carry on, will exception out or something below) } } //TESTED //read back result BufferedReader inStream = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); StringBuilder strBuilder = new StringBuilder(); String buffer; while ((buffer = inStream.readLine()) != null) { strBuilder.append(buffer); } inStream.close(); //save cookie if cookie is null if (cookie == null) { String headername; for (int i = 1; (headername = urlConnection.getHeaderFieldKey(i)) != null; i++) { if (headername.equals("Set-Cookie")) { cookie = urlConnection.getHeaderField(i); break; } } } return strBuilder.toString(); }
From source file:org.kuali.test.utils.Utils.java
/** * * @param status/*from www.j a v a 2s . c o m*/ * @return */ public static boolean isHttpRedirect(int status) { return ((status == HttpURLConnection.HTTP_MOVED_TEMP) || (status == HttpURLConnection.HTTP_MOVED_PERM) || (status == HttpURLConnection.HTTP_SEE_OTHER)); }
From source file:uk.ac.vfb.geppetto.VFBProcessTermInfo.java
/** * @param urlString//from www . ja va2 s.c o m */ private String checkURL(String urlString) { try { urlString = urlString.replace("https://", "http://").replace(":5000", ""); urlString = urlString.replace("//virtualflybrain.org", "//www.virtualflybrain.org"); URL url = new URL(urlString); HttpURLConnection huc = (HttpURLConnection) url.openConnection(); huc.setRequestMethod("HEAD"); huc.setInstanceFollowRedirects(true); int response = huc.getResponseCode(); if (response == HttpURLConnection.HTTP_OK) { return urlString; } else if (response == HttpURLConnection.HTTP_MOVED_TEMP || response == HttpURLConnection.HTTP_MOVED_PERM) { return checkURL(huc.getHeaderField("Location")); } return null; } catch (Exception e) { System.out.println("Error checking url (" + urlString + ") " + e.toString()); return null; } }