List of usage examples for java.net HttpURLConnection setInstanceFollowRedirects
public void setInstanceFollowRedirects(boolean followRedirects)
From source file:org.jasig.cas.util.HttpClient.java
public boolean isValidEndPoint(final URL url) { HttpURLConnection connection = null; InputStream is = null;//from ww w. j ava 2 s. c om try { connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(this.connectionTimeout); connection.setReadTimeout(this.readTimeout); connection.setInstanceFollowRedirects(this.followRedirects); if (connection instanceof HttpsURLConnection) { final HttpsURLConnection httpsConnection = (HttpsURLConnection) connection; if (this.sslSocketFactory != null) { httpsConnection.setSSLSocketFactory(this.sslSocketFactory); } if (this.hostnameVerifier != null) { httpsConnection.setHostnameVerifier(this.hostnameVerifier); } } connection.connect(); final int responseCode = connection.getResponseCode(); for (final int acceptableCode : this.acceptableCodes) { if (responseCode == acceptableCode) { LOGGER.debug("Response code from server matched {}.", responseCode); return true; } } LOGGER.debug("Response Code did not match any of the acceptable response codes. Code returned was {}", responseCode); // if the response code is an error and we don't find that error acceptable above: if (responseCode == 500) { is = connection.getInputStream(); final String value = IOUtils.toString(is); LOGGER.error("There was an error contacting the endpoint: {}; The error was:\n{}", url.toExternalForm(), value); } } catch (final IOException e) { LOGGER.error(e.getMessage(), e); } finally { IOUtils.closeQuietly(is); if (connection != null) { connection.disconnect(); } } return false; }
From source file:net.solarnetwork.node.control.ping.HttpRequesterJob.java
private boolean ping() { log.debug("Attempting to ping {}", url); try {//from ww w . j a va 2s.c o m HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setConnectTimeout(connectionTimeoutSeconds * 1000); connection.setReadTimeout(connectionTimeoutSeconds * 1000); connection.setRequestMethod("HEAD"); connection.setInstanceFollowRedirects(false); if (sslService != null && connection instanceof HttpsURLConnection) { SSLService service = sslService.service(); if (service != null) { SSLSocketFactory factory = service.getSolarInSocketFactory(); if (factory != null) { HttpsURLConnection sslConnection = (HttpsURLConnection) connection; sslConnection.setSSLSocketFactory(factory); } } } int responseCode = connection.getResponseCode(); return (responseCode >= 200 && responseCode < 400); } catch (IOException e) { log.info("Error pinging {}: {}", url, e.getMessage()); return false; } }
From source file:org.jenkinsci.plugins.codefresh.CFApi.java
public HttpURLConnection getConnection(String urlString) throws MalformedURLException, IOException { if (urlString.isEmpty()) { urlString = cfUrl;/*w w w . ja v a 2s. co m*/ } URL connUrl = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) connUrl.openConnection(); conn.setRequestProperty("x-access-token", cfToken.getPlainText()); conn.setUseCaches(false); conn.setDoOutput(true); conn.setDoInput(true); conn.setInstanceFollowRedirects(true); if (https) { HttpsURLConnection.setFollowRedirects(true); return (HttpsURLConnection) conn; } return conn; }
From source file:org.openbravo.test.webservice.BaseWSTest.java
/** * Creates a HTTP connection.//w w w. ja v a 2 s .c o m * * @param wsPart * @param method * POST, PUT, GET or DELETE * @return the created connection * @throws Exception */ protected HttpURLConnection createConnection(String wsPart, String method) throws Exception { Authenticator.setDefault(new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(LOGIN, PWD.toCharArray()); } }); log.debug(method + ": " + getOpenbravoURL() + wsPart); final URL url = new URL(getOpenbravoURL() + wsPart); final HttpURLConnection hc = (HttpURLConnection) url.openConnection(); hc.setRequestMethod(method); hc.setAllowUserInteraction(false); hc.setDefaultUseCaches(false); hc.setDoOutput(true); hc.setDoInput(true); hc.setInstanceFollowRedirects(true); hc.setUseCaches(false); hc.setRequestProperty("Content-Type", "text/xml"); return hc; }
From source file:nu.nethome.home.items.hue.JsonRestClient.java
private String performRequest(String baseUrl, String resource, String body, String method) throws IOException { HttpURLConnection connection = null; DataOutputStream wr = null;//from w ww. ja v a2 s.c o m BufferedReader rd = null; StringBuilder sb = new StringBuilder(); String line; URL serverAddress; try { serverAddress = new URL(baseUrl + resource); //Set up the initial connection connection = (HttpURLConnection) serverAddress.openConnection(); connection.setRequestMethod(method); connection.setDoOutput(true); connection.setReadTimeout(10000); connection.setInstanceFollowRedirects(false); connection.setRequestProperty("Content-Type", "application/json"); connection.setUseCaches(false); if (body.length() > 0) { connection.setRequestProperty("Content-Length", "" + Integer.toString(body.getBytes().length)); wr = new DataOutputStream(connection.getOutputStream()); wr.writeBytes(body); wr.flush(); wr.close(); } connection.connect(); if (connection.getResponseCode() < 200 || connection.getResponseCode() > 299) { throw new ProtocolException("Bad HTTP response code: " + connection.getResponseCode()); } rd = new BufferedReader(new InputStreamReader(connection.getInputStream())); while ((line = rd.readLine()) != null) { sb.append(line); sb.append('\n'); } } finally { if (rd != null) { rd.close(); } if (wr != null) { wr.close(); } if (connection != null) { connection.disconnect(); } } return sb.toString(); }
From source file:nu.nethome.home.items.web.proxy.JsonRestClient.java
private String performRequest(String baseUrl, String resource, String body, String method) throws IOException { HttpURLConnection connection = null; DataOutputStream wr = null;/*from w w w .jav a2 s. co m*/ BufferedReader rd = null; StringBuilder sb = new StringBuilder(); String line; URL serverAddress; try { serverAddress = new URL(baseUrl + resource); //Set up the initial connection connection = (HttpURLConnection) serverAddress.openConnection(); connection.setRequestMethod(method); connection.setDoOutput(true); connection.setReadTimeout(15000); connection.setInstanceFollowRedirects(false); connection.setRequestProperty("Content-Type", "application/json"); connection.setUseCaches(false); if (body.length() > 0) { connection.setRequestProperty("Content-Length", "" + Integer.toString(body.getBytes().length)); wr = new DataOutputStream(connection.getOutputStream()); wr.writeBytes(body); wr.flush(); wr.close(); } connection.connect(); if (connection.getResponseCode() < 200 || connection.getResponseCode() > 299) { throw new ProtocolException("Bad HTTP response code: " + connection.getResponseCode()); } rd = new BufferedReader(new InputStreamReader(connection.getInputStream())); while ((line = rd.readLine()) != null) { sb.append(line); sb.append('\n'); } } finally { if (rd != null) { rd.close(); } if (wr != null) { wr.close(); } if (connection != null) { connection.disconnect(); } } return sb.toString(); }
From source file:org.sakaiproject.commons.tool.entityprovider.CommonsEntityProvider.java
@EntityCustomAction(action = "getUrlMarkup", viewKey = EntityView.VIEW_LIST) public ActionReturn getUrlMarkup(OutputStream outputStream, EntityView view, Map<String, Object> params) { String userId = getCheckedUser(); String urlString = (String) params.get("url"); if (StringUtils.isBlank(urlString)) { throw new EntityException("No url supplied", "", HttpServletResponse.SC_BAD_REQUEST); }/* w w w. j a v a 2s . c om*/ try { CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL)); URL url = new URL(urlString); URLConnection c = url.openConnection(); if (c instanceof HttpURLConnection) { HttpURLConnection conn = (HttpURLConnection) c; conn.setRequestProperty("User-Agent", USER_AGENT); conn.setInstanceFollowRedirects(false); conn.connect(); String contentEncoding = conn.getContentEncoding(); String contentType = conn.getContentType(); int responseCode = conn.getResponseCode(); log.debug("Response code: {}", responseCode); int redirectCounter = 1; while ((responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == HttpURLConnection.HTTP_SEE_OTHER) && redirectCounter < 20) { String newUri = conn.getHeaderField("Location"); log.debug("{}. New URI: {}", responseCode, newUri); String cookies = conn.getHeaderField("Set-Cookie"); url = new URL(newUri); c = url.openConnection(); conn = (HttpURLConnection) c; conn.setInstanceFollowRedirects(false); conn.setRequestProperty("User-Agent", USER_AGENT); conn.setRequestProperty("Cookie", cookies); conn.connect(); contentEncoding = conn.getContentEncoding(); contentType = conn.getContentType(); responseCode = conn.getResponseCode(); log.debug("Redirect counter: {}", redirectCounter); log.debug("Response code: {}", responseCode); redirectCounter += 1; } if (contentType != null && (contentType.startsWith("text/html") || contentType.startsWith("application/xhtml+xml") || contentType.startsWith("application/xml"))) { String mimeType = contentType.split(";")[0].trim(); log.debug("mimeType: {}", mimeType); log.debug("encoding: {}", contentEncoding); BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream)); String line = null; while ((line = reader.readLine()) != null) { writer.write(line); } return new ActionReturn(contentEncoding, mimeType, outputStream); } else { log.debug("Invalid content type {}. Throwing bad request ...", contentType); throw new EntityException("Url content type not supported", "", HttpServletResponse.SC_BAD_REQUEST); } } else { throw new EntityException("Url content type not supported", "", HttpServletResponse.SC_BAD_REQUEST); } } catch (MalformedURLException mue) { throw new EntityException("Invalid url supplied", "", HttpServletResponse.SC_BAD_REQUEST); } catch (IOException ioe) { throw new EntityException("Failed to download url contents", "", HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } }
From source file:org.pocketcampus.plugin.moodle.server.old.MoodleServiceImpl.java
private HttpPageReply getHttpReplyWithCookie(String url, Cookie cookie) throws IOException { HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setRequestProperty("User-Agent", "Mozilla/5.0"); conn.setInstanceFollowRedirects(false); conn.setRequestProperty("Cookie", cookie.cookie()); if (conn.getResponseCode() == 200) return new HttpPageReply(StringUtils.fromStream(conn.getInputStream(), "UTF-8"), null); if (conn.getResponseCode() / 100 == 3) return new HttpPageReply(null, conn.getHeaderField("Location")); return new HttpPageReply(null, null); }
From source file:URLTree.FindOptimalPath.java
public void printMap(List<Node> node, int stage, String outputFile, int threshold, String ouptputFrequencyFile, String filed3) {//ww w. ja v a 2s. c o m Map<String, Integer> countSimilarNode = new HashMap<String, Integer>(); String nodeMatrix[][] = new String[node.size()][stage]; for (int i = 0; i < node.size(); i++) { //System.out.println(Arrays.toString(node.get(i).getNodeArr())); String arr[] = node.get(i).getNodeArr(); for (int j = 0; j < arr.length; j++) { if (j < stage) { nodeMatrix[i][j] = arr[j]; } } } List<MergeSimilarNode> similarNode = new ArrayList<MergeSimilarNode>(); Map<String, Integer> wordMap = new HashMap<String, Integer>(); for (int i = 0; i < node.size(); i++) { for (int j = 0; j < stage; j++) { if (nodeMatrix[i][j] != null) { FreqWords nodeFreq = new FreqWords(); System.out.print("[" + i + j + "]: " + nodeMatrix[i][j]); if (wordMap.containsKey(nodeMatrix[i][j] + "," + j)) { wordMap.put(nodeMatrix[i][j] + "," + j, wordMap.get(nodeMatrix[i][j] + "," + j) + 1); } else { wordMap.put(nodeMatrix[i][j] + "," + j, 1); } } } System.out.println(); } List<FreqWords> freq = new ArrayList<FreqWords>(); for (Map.Entry<String, Integer> entry : wordMap.entrySet()) { FreqWords fwords = new FreqWords(); String key = entry.getKey(); Integer value = entry.getValue(); //String nodeStr[]=key.split(","); String[] nodeStr = new String[2]; StringTokenizer st = new StringTokenizer(key, ","); int k = 0; while (st.hasMoreTokens()) { nodeStr[k] = st.nextToken(); k++; } fwords.setNode(nodeStr[0]); System.out.println("node freq: " + nodeStr[1]); if (nodeStr[1] != null) { fwords.setFreq(Integer.parseInt(nodeStr[1])); } else { System.out.println("null value at" + nodeStr[0] + ": " + nodeStr[1]); } fwords.setValue(value); freq.add(fwords); } System.out.println("Done Matrix"); //System.out.println(); PrintWriter writer = null; List<String> urlList = new ArrayList<String>(); List<FilterURL> varifiedList = new ArrayList<FilterURL>(); try { writer = new PrintWriter(outputFile, "UTF-8"); for (int i = 0; i < node.size(); i++) { String urlArr[] = new String[stage]; int flag = 0; int sum = 0; for (int j = 0; j < stage; j++) { if (nodeMatrix[i][j] != null) { for (FreqWords words : freq) { //System.out.println(); //if(words.getNode().equals("ca")) // System.out.println("words: "+words.getNode()+" i: "+i+"j: "+j+" value: "+nodeMatrix[i][j]+" word Freq: "+ words.getFreq()); if (words.getNode().equals(nodeMatrix[i][j])) { //if(words.getFreq()!=0) //{ if (words.getFreq() == j) { int count = node.get(i).getCount(); //url=url.append(nodeMatrix[i][j]).append("(").append(value+node.get(i).getCount()).append(")-"); if (threshold > words.getValue()) { flag = 1; break; } else { sum += count; urlArr[j] = nodeMatrix[i][j]; // System.out.println("i: "+i+"j: "+j+" found: "+urlArr[j]); break; //System.out.print(nodeMatrix[i][j]+"("+value+")"); } } //} } } } if (flag == 1) { flag = 0; break; } } //String urldata=StringUtils.join(urlArr,"-"); String urldata = Joiner.on("-").skipNulls().join(urlArr).trim(); //System.out.println("Mearge URL"+urldata); if (urldata.endsWith("-")) { urldata = urldata.substring(0, urldata.length() - 1); } urldata = StringUtils.stripEnd(urldata, null); // System.out.print(urldata); if (!urldata.isEmpty()) { if (urldata.contains("-")) { // writer.println(urldata+","+sum); FilterURL filter = new FilterURL(); filter.setReversedURL(urldata); filter.setCount(sum); varifiedList.add(filter); } } //System.out.println(i); // System.out.println(i); } //varifiedSequece(varifiedList,writer); for (int i = 0; i < varifiedList.size() - 1; i++) { if (varifiedList.get(i).getReversedURL().contains(varifiedList.get(i + 1).getReversedURL())) { } else { if (threshold != 0) { String nodesName[] = varifiedList.get(i).getReversedURL().split("-"); if (nodesName.length == 2) { try { String url2Domain = "http://" + nodesName[1] + "." + nodesName[0]; URL url = new URL(url2Domain); // open connection HttpURLConnection httpURLConnection = (HttpURLConnection) url .openConnection(Proxy.NO_PROXY); // stop following browser redirect httpURLConnection.setInstanceFollowRedirects(false); httpURLConnection.setConnectTimeout(15000); httpURLConnection.setReadTimeout(15000); // extract location header containing the actual destination URL String expandedURL = httpURLConnection.getHeaderField("Location"); httpURLConnection.disconnect(); if (expandedURL != null) { System.out.println("Correct: " + expandedURL); writer.println(varifiedList.get(i).getReversedURL() + "," + varifiedList.get(i).getCount()); } } catch (Exception e) { System.out.println("Incorrect: " + e); } } else { writer.println( varifiedList.get(i).getReversedURL() + "," + varifiedList.get(i).getCount()); } } else { writer.println(varifiedList.get(i).getReversedURL() + "," + varifiedList.get(i).getCount()); } } } } catch (Exception e) { e.printStackTrace(); System.out.println(e); } finally { if (writer != null) { writer.close(); // **** closing it flushes it and reclaims resources **** } } System.out.println("Write into File->" + outputFile); Path obj = new Path(); obj.finalFrequency(outputFile, ouptputFrequencyFile); // obj.preSunbrust(ouptputFrequencyFile,filed3); }
From source file:net.sf.golly.HelpActivity.java
private String downloadURL(String urlstring, String filepath) { // download given url and save data in given file try {/*from w w w.j av a 2s . c o m*/ File outfile = new File(filepath); final int BUFFSIZE = 8192; FileOutputStream outstream = null; try { outstream = new FileOutputStream(outfile); } catch (FileNotFoundException e) { return "File not found: " + filepath; } long starttime = System.nanoTime(); // Log.i("downloadURL: ", urlstring); URL url = new URL(urlstring); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setAllowUserInteraction(false); connection.setInstanceFollowRedirects(true); connection.setRequestMethod("GET"); connection.connect(); if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { outstream.close(); return "No HTTP_OK response."; } // init info for progress bar int filesize = connection.getContentLength(); int downloaded = 0; int percent; int lastpercent = 0; // stream the data to given file InputStream instream = connection.getInputStream(); byte[] buffer = new byte[BUFFSIZE]; int bufflen = 0; while ((bufflen = instream.read(buffer, 0, BUFFSIZE)) > 0) { outstream.write(buffer, 0, bufflen); downloaded += bufflen; percent = (int) ((downloaded / (float) filesize) * 100); if (percent > lastpercent) { progbar.setProgress(percent); lastpercent = percent; } // show proglayout only if download takes more than 1 second if (System.nanoTime() - starttime > 1000000000L) { runOnUiThread(new Runnable() { public void run() { proglayout.setVisibility(LinearLayout.VISIBLE); } }); starttime = Long.MAX_VALUE; // only show it once } if (cancelled) break; } outstream.close(); connection.disconnect(); if (cancelled) return "Cancelled."; } catch (MalformedURLException e) { return "Bad URL string: " + urlstring; } catch (IOException e) { return "Could not connect to URL: " + urlstring; } return ""; // success }