List of usage examples for java.net HttpURLConnection setInstanceFollowRedirects
public void setInstanceFollowRedirects(boolean followRedirects)
From source file:vocab.VocabUtils.java
/** * Jena fails to load models in https with content negotiation. Therefore I do * the negotiation here directly//from w w w. j av a2 s .c o m */ private static void doContentNegotiation(OntModel model, Vocabulary v, String accept, String serialization) { try { model.read(v.getUri(), null, serialization); } catch (Exception e) { try { System.out.println("Failed to read the ontology. Doing content negotiation"); URL url = new URL(v.getUri()); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setInstanceFollowRedirects(true); connection.setRequestProperty("Accept", accept); int status = connection.getResponseCode(); if (status == HttpURLConnection.HTTP_SEE_OTHER || status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM) { String newUrl = connection.getHeaderField("Location"); //v.setUri(newUrl); connection = (HttpURLConnection) new URL(newUrl).openConnection(); connection.setRequestProperty("Accept", accept); InputStream in = (InputStream) connection.getInputStream(); model.read(in, null, serialization); } } catch (Exception e2) { System.out.println("Failed to read the ontology"); } } }
From source file:de.langerhans.wallet.ExchangeRatesProvider.java
private static Map<String, ExchangeRate> requestExchangeRates(final URL url, double dogeBtcConversion, final String userAgent, final String source, final String... fields) { final long start = System.currentTimeMillis(); HttpURLConnection connection = null; Reader reader = null;//from ww w . j a v a2 s.c o m try { connection = (HttpURLConnection) url.openConnection(); connection.setInstanceFollowRedirects(false); connection.setConnectTimeout(Constants.HTTP_TIMEOUT_MS); connection.setReadTimeout(Constants.HTTP_TIMEOUT_MS); connection.addRequestProperty("User-Agent", userAgent); connection.addRequestProperty("Accept-Encoding", "gzip"); connection.connect(); final int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { final String contentEncoding = connection.getContentEncoding(); InputStream is = new BufferedInputStream(connection.getInputStream(), 1024); if ("gzip".equalsIgnoreCase(contentEncoding)) is = new GZIPInputStream(is); reader = new InputStreamReader(is, Charsets.UTF_8); final StringBuilder content = new StringBuilder(); final long length = Io.copy(reader, content); final Map<String, ExchangeRate> rates = new TreeMap<String, ExchangeRate>(); final JSONObject head = new JSONObject(content.toString()); for (final Iterator<String> i = head.keys(); i.hasNext();) { final String currencyCode = i.next(); if (!"timestamp".equals(currencyCode)) { final JSONObject o = head.getJSONObject(currencyCode); for (final String field : fields) { final String rate = o.optString(field, null); if (rate != null) { try { final double btcRate = Double .parseDouble(Fiat.parseFiat(currencyCode, rate).toPlainString()); DecimalFormat df = new DecimalFormat("#.########"); df.setRoundingMode(RoundingMode.HALF_UP); DecimalFormatSymbols dfs = new DecimalFormatSymbols(); dfs.setDecimalSeparator('.'); dfs.setGroupingSeparator(','); df.setDecimalFormatSymbols(dfs); final Fiat dogeRate = Fiat.parseFiat(currencyCode, df.format(btcRate * dogeBtcConversion)); if (dogeRate.signum() > 0) { rates.put(currencyCode, new ExchangeRate( new com.dogecoin.dogecoinj.utils.ExchangeRate(dogeRate), source)); break; } } catch (final NumberFormatException x) { log.warn("problem fetching {} exchange rate from {} ({}): {}", currencyCode, url, contentEncoding, x.getMessage()); } } } } } log.info("fetched exchange rates from {} ({}), {} chars, took {} ms", url, contentEncoding, length, System.currentTimeMillis() - start); return rates; } else { log.warn("http status {} when fetching exchange rates from {}", responseCode, url); } } catch (final Exception x) { log.warn("problem fetching exchange rates from " + url, x); } finally { if (reader != null) { try { reader.close(); } catch (final IOException x) { // swallow } } if (connection != null) connection.disconnect(); } return null; }
From source file:piuk.blockchain.android.util.WalletUtils.java
public static String postURL(String request, String urlParameters) throws Exception { String error = null;/*ww w.j a va 2 s . com*/ for (int ii = 0; ii < DefaultRequestRetry; ++ii) { URL url = new URL(request); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); try { connection.setDoOutput(true); connection.setDoInput(true); connection.setInstanceFollowRedirects(false); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("charset", "utf-8"); connection.setRequestProperty("Accept", "application/json"); connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length)); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36"); connection.setUseCaches(false); connection.setConnectTimeout(DefaultRequestTimeout); connection.setReadTimeout(DefaultRequestTimeout); connection.connect(); DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); wr.writeBytes(urlParameters); wr.flush(); wr.close(); connection.setInstanceFollowRedirects(false); if (connection.getResponseCode() == 200) { // Log.d("postURL", "return code 200"); return IOUtils.toString(connection.getInputStream(), "UTF-8"); } else { error = IOUtils.toString(connection.getErrorStream(), "UTF-8"); // Log.d("postURL", "return code " + error); } Thread.sleep(5000); } finally { connection.disconnect(); } } throw new Exception("Inavlid Response " + error); }
From source file:org.dcm4che3.tool.stowrs.StowRS.java
private static StowRSResponse sendDicomFile(String url, File f) throws IOException { int rspCode = 0; String rspMessage = null;//ww w . ja v a 2 s .c om URL newUrl = new URL(url); HttpURLConnection connection = (HttpURLConnection) newUrl.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setInstanceFollowRedirects(false); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "multipart/related; type=application/dicom; boundary=" + MULTIPART_BOUNDARY); connection.setRequestProperty("Accept", "application/dicom+xml"); connection.setRequestProperty("charset", "utf-8"); connection.setUseCaches(false); DataOutputStream wr; wr = new DataOutputStream(connection.getOutputStream()); wr.writeBytes("\r\n--" + MULTIPART_BOUNDARY + "\r\n"); wr.writeBytes("Content-Disposition: inline; name=\"file[]\"; filename=\"" + f.getName() + "\"\r\n"); wr.writeBytes("Content-Type: application/dicom \r\n"); wr.writeBytes("\r\n"); FileInputStream fis = new FileInputStream(f); StreamUtils.copy(fis, wr); fis.close(); wr.writeBytes("\r\n--" + MULTIPART_BOUNDARY + "--\r\n"); wr.flush(); wr.close(); String response = connection.getResponseMessage(); rspCode = connection.getResponseCode(); rspMessage = connection.getResponseMessage(); LOG.info("response: " + response); Attributes responseAttrs = null; try { InputStream in; boolean isErrorCase = rspCode >= HttpURLConnection.HTTP_BAD_REQUEST; if (!isErrorCase) { in = connection.getInputStream(); } else { in = connection.getErrorStream(); } if (!isErrorCase || rspCode == HttpURLConnection.HTTP_CONFLICT) responseAttrs = SAXReader.parse(in); } catch (SAXException e) { throw new IOException(e); } catch (ParserConfigurationException e) { throw new IOException(e); } connection.disconnect(); return new StowRSResponse(rspCode, rspMessage, responseAttrs); }
From source file:net.technicpack.launchercore.util.Utils.java
/** * Establishes an HttpURLConnection from a URL, with the correct configuration to receive content from the given URL. * * @param url The URL to set up and receive content from * @return A valid HttpURLConnection/*from w w w. j a v a 2 s . c om*/ * * @throws IOException The openConnection() method throws an IOException and the calling method is responsible for handling it. */ public static HttpURLConnection openHttpConnection(URL url) throws IOException { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.setDoOutput(false); System.setProperty("http.agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19"); conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19"); HttpURLConnection.setFollowRedirects(true); conn.setUseCaches(false); conn.setInstanceFollowRedirects(true); return conn; }
From source file:net.technicpack.launchercore.util.DownloadUtils.java
public static String getETag(String address) { String md5 = ""; try {//w w w. j a va 2 s .c o m URL url = new URL(address); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.setDoOutput(false); System.setProperty("http.agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19"); conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19"); HttpURLConnection.setFollowRedirects(true); conn.setUseCaches(false); conn.setInstanceFollowRedirects(true); String eTag = conn.getHeaderField("ETag"); if (eTag != null) { eTag = eTag.replaceAll("^\"|\"$", ""); if (eTag.length() == 32) { md5 = eTag; } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return md5; }
From source file:BihuHttpUtil.java
/** * ??HTTPJSON?//from w w w . j a v a2s. c o m * @param url * @param jsonStr */ public static String sendPostForJson(String url, String jsonStr) { StringBuffer sb = new StringBuffer(""); try { // URL realUrl = new URL(url); HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); connection.setUseCaches(false); connection.setInstanceFollowRedirects(true); connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); connection.connect(); //POST DataOutputStream out = new DataOutputStream(connection.getOutputStream()); out.write(jsonStr.getBytes("UTF-8"));//??? out.flush(); out.close(); //?? BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String lines; while ((lines = reader.readLine()) != null) { lines = new String(lines.getBytes(), "utf-8"); sb.append(lines); } reader.close(); // connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } return sb.toString(); }
From source file:org.rapidcontext.app.plugin.http.HttpPostProcedure.java
/** * Creates an HTTP connection for the specified URL and headers. * * @param url the URL to use/*from w w w. jav a2 s . c o m*/ * @param headers the additional HTTP headers * * @return the HTTP connection created * * @throws ProcedureException if the connection couldn't be created */ private static HttpURLConnection createConnection(URL url, Map headers) throws ProcedureException { HttpURLConnection con; String msg; try { con = (HttpURLConnection) url.openConnection(); } catch (IOException e) { msg = "failed to open URL " + url + ":" + e.getMessage(); throw new ProcedureException(msg); } con.setDoInput(true); con.setAllowUserInteraction(false); con.setUseCaches(false); con.setInstanceFollowRedirects(false); con.setConnectTimeout(10000); con.setReadTimeout(45000); con.setRequestProperty("Cache-Control", "no-cache"); con.setRequestProperty("Accept", "text/*, application/*"); con.setRequestProperty("Accept-Charset", "UTF-8"); con.setRequestProperty("Accept-Encoding", "identity"); // TODO: Extract correct version number from JAR file con.setRequestProperty("User-Agent", "RapidContext/1.0"); Iterator iter = headers.keySet().iterator(); while (iter.hasNext()) { String str = (String) iter.next(); con.setRequestProperty(str, (String) headers.get(str)); } return con; }
From source file:edu.stanford.muse.index.NER.java
/** returns a list of <name, #occurrences> */ public static List<Pair<String, Float>> namesFromURL(String url, boolean removeCommonNames) throws ClassCastException, IOException, ClassNotFoundException { // only http conns allowed currently Indexer.log.info(url);//ww w .j ava 2 s . c om HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setInstanceFollowRedirects(true); conn.setRequestProperty("User-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:6.0.2) Gecko/20100101 Firefox/6.0.2"); conn.connect(); Indexer.log.info("url for extracting names:" + conn.getURL()); byte[] b = Util.getBytesFromStream(conn.getInputStream()); String text = new String(b, "UTF-8"); text = Util.unescapeHTML(text); org.jsoup.nodes.Document doc = Jsoup.parse(text); text = doc.body().text(); return namesFromText(text, removeCommonNames); }
From source file:edu.stanford.muse.index.NER.java
public static List<Pair<String, Float>> namesFromArchive(String url, boolean removeCommonNames) throws ClassCastException, IOException, ClassNotFoundException { // only http conns allowed currently HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setInstanceFollowRedirects(true); conn.setRequestProperty("User-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:6.0.2) Gecko/20100101 Firefox/6.0.2"); conn.connect();//from www. j a va 2 s . com byte[] b = Util.getBytesFromStream(conn.getInputStream()); String text = new String(b, "UTF-8"); text = Util.unescapeHTML(text); org.jsoup.nodes.Document doc = Jsoup.parse(text); text = doc.body().text(); return namesFromText(text, removeCommonNames); }