List of usage examples for java.net HttpURLConnection setRequestProperty
public void setRequestProperty(String key, String value)
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();// w w w.ja v a2s.c o m 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:com.github.codingtogenomic.CodingToGenomic.java
private static String getContent(final String endpoint) throws MalformedURLException, IOException, InterruptedException { if (requestCount == 15) { // check every 15 final long currentTime = System.currentTimeMillis(); final long diff = currentTime - lastRequestTime; //if less than a second then sleep for the remainder of the second if (diff < 1000) { Thread.sleep(1000 - diff); }//from ww w. j a v a 2s. co m //reset lastRequestTime = System.currentTimeMillis(); requestCount = 0; } final URL url = new URL(SERVER + endpoint); URLConnection connection = url.openConnection(); HttpURLConnection httpConnection = (HttpURLConnection) connection; httpConnection.setRequestProperty("Content-Type", "application/json"); final InputStream response = httpConnection.getInputStream(); int responseCode = httpConnection.getResponseCode(); if (responseCode != 200) { if (responseCode == 429 && httpConnection.getHeaderField("Retry-After") != null) { double sleepFloatingPoint = Double.valueOf(httpConnection.getHeaderField("Retry-After")); double sleepMillis = 1000 * sleepFloatingPoint; Thread.sleep((long) sleepMillis); return getContent(endpoint); } throw new RuntimeException("Response code was not 200. Detected response was " + responseCode); } String output; Reader reader = null; try { reader = new BufferedReader(new InputStreamReader(response, "UTF-8")); StringBuilder builder = new StringBuilder(); char[] buffer = new char[8192]; int read; while ((read = reader.read(buffer, 0, buffer.length)) > 0) { builder.append(buffer, 0, read); } output = builder.toString(); } finally { if (reader != null) { try { reader.close(); } catch (IOException logOrIgnore) { logOrIgnore.printStackTrace(); } } } return output; }
From source file:org.wso2.carbon.appmanager.integration.ui.Util.HttpUtil.java
public static HttpResponse doGet(String endpoint, Map<String, String> headers) throws IOException { HttpResponse httpResponse;/* ww w. j av a2 s . com*/ if (endpoint.startsWith("http://")) { URL url = new URL(endpoint); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setDoOutput(true); conn.setReadTimeout(30000); // setting headers if (headers != null && headers.size() > 0) { Iterator<String> itr = headers.keySet().iterator(); while (itr.hasNext()) { String key = itr.next(); conn.setRequestProperty(key, headers.get(key)); } } conn.connect(); // Get the response StringBuilder sb = new StringBuilder(); BufferedReader rd = null; try { rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = rd.readLine()) != null) { sb.append(line); } httpResponse = new HttpResponse(sb.toString(), conn.getResponseCode()); httpResponse.setResponseMessage(conn.getResponseMessage()); } catch (IOException ignored) { rd = new BufferedReader(new InputStreamReader(conn.getErrorStream())); String line; while ((line = rd.readLine()) != null) { sb.append(line); } httpResponse = new HttpResponse(sb.toString(), conn.getResponseCode()); httpResponse.setResponseMessage(conn.getResponseMessage()); } finally { if (rd != null) { rd.close(); } } return httpResponse; } return null; }
From source file:com.moviejukebox.plugin.OpenSubtitlesPlugin.java
private static boolean subtitleDownload(Movie movie, File movieFile, File subtitleFile) { try {/*from w w w. j av a 2s. c o m*/ String ret; String xml; String moviehash = getHash(movieFile); String moviebytesize = String.valueOf(movieFile.length()); xml = generateXMLRPCSS(moviehash, moviebytesize); ret = sendRPC(xml); String subDownloadLink = getValue("SubDownloadLink", ret); if (StringUtils.isBlank(subDownloadLink)) { String moviefilename = movieFile.getName(); // Do not search by file name for BD rip files in the format 0xxxx.m2ts if (!(moviefilename.toUpperCase().endsWith(".M2TS") && moviefilename.startsWith("0"))) { // Try to find the subtitle using file name String subfilename = subtitleFile.getName(); int index = subfilename.lastIndexOf('.'); String query = subfilename.substring(0, index); xml = generateXMLRPCSS(query); ret = sendRPC(xml); subDownloadLink = getValue("SubDownloadLink", ret); } } if (StringUtils.isBlank(subDownloadLink)) { LOG.debug("Subtitle not found for {}", movieFile.getName()); return Boolean.FALSE; } LOG.debug("Download subtitle for {}", movie.getBaseName()); URL url = new URL(subDownloadLink); HttpURLConnection connection = (HttpURLConnection) (url .openConnection(YamjHttpClientBuilder.getProxy())); connection.setRequestProperty("Connection", "Close"); try (InputStream inputStream = connection.getInputStream()) { int code = connection.getResponseCode(); if (code != HttpURLConnection.HTTP_OK) { LOG.error("Download Failed"); return Boolean.FALSE; } FileTools.copy(inputStream, new FileOutputStream(subtitleFile)); } finally { connection.disconnect(); } String subLanguageID = getValue("SubLanguageID", ret); if (StringUtils.isNotBlank(subLanguageID)) { SubtitleTools.addMovieSubtitle(movie, subLanguageID); } else { SubtitleTools.addMovieSubtitle(movie, "YES"); } return Boolean.TRUE; } catch (Exception ex) { LOG.error("Download Exception (Movie Not Found)", ex); return Boolean.FALSE; } }
From source file:eu.geopaparazzi.library.network.NetworkUtilities.java
/** * Sends a string via POST to a given url. * /*from w w w.jav a2s.c om*/ * @param urlStr the url to which to send to. * @param string the string to send as post body. * @param user the user or <code>null</code>. * @param password the password or <code>null</code>. * @return the response. * @throws Exception */ public static String sendPost(String urlStr, String string, String user, String password) throws Exception { BufferedOutputStream wr = null; HttpURLConnection conn = null; try { conn = makeNewConnection(urlStr); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setDoInput(true); // conn.setChunkedStreamingMode(0); conn.setUseCaches(false); if (user != null && password != null) { conn.setRequestProperty("Authorization", getB64Auth(user, password)); } conn.connect(); // Make server believe we are form data... wr = new BufferedOutputStream(conn.getOutputStream()); byte[] bytes = string.getBytes(); wr.write(bytes); wr.flush(); int responseCode = conn.getResponseCode(); StringBuilder returnMessageBuilder = new StringBuilder(); if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8")); while (true) { String line = br.readLine(); if (line == null) break; returnMessageBuilder.append(line + "\n"); } br.close(); } return returnMessageBuilder.toString(); } catch (Exception e) { e.printStackTrace(); throw e; } finally { if (conn != null) conn.disconnect(); } }
From source file:br.bireme.tb.URLS.java
/** * Given an url, loads its content (POST - method) * @param url url to be loaded/*from w w w. ja v a2s. c om*/ * @param urlParameters post parameters * @return an array with the real location of the page (in case of redirect) * and its content. * @throws IOException */ public static String[] loadPagePost(final URL url, final String urlParameters) throws IOException { if (url == null) { throw new NullPointerException("url"); } if (urlParameters == null) { throw new NullPointerException("urlParameters"); } final String encodedParams = URLEncoder.encode(urlParameters, DEFAULT_ENCODING); //System.out.print("loading page (POST): [" + url + "] params: " + urlParameters); //Create connection final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("Content-Length", "" + Integer.toString(encodedParams.getBytes().length)); //.getBytes(DEFAULT_ENCODING).length)); connection.setRequestProperty("Content-Language", "pt-BR"); connection.setUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) { wr.write(encodedParams.getBytes(DEFAULT_ENCODING)); //wr.writeBytes(urlParameters); wr.flush(); } //Get Response final StringBuffer response = new StringBuffer(); try (final BufferedReader rd = new BufferedReader( new InputStreamReader(connection.getInputStream(), DEFAULT_ENCODING))) { while (true) { final String line = rd.readLine(); if (line == null) { break; } response.append(line); response.append('\n'); } } connection.disconnect(); return new String[] { url.toString() + "?" + urlParameters, response.toString() }; }
From source file:edu.jhu.cvrg.timeseriesstore.opentsdb.TimeSeriesUtility.java
private static HttpURLConnection openHTTPConnection(String urlString, HttpVerbs verb) throws MalformedURLException, IOException { URL url = null;/*from w w w . j av a 2s. c o m*/ HttpURLConnection conn = null; url = new URL(urlString); conn = (HttpURLConnection) url.openConnection(); switch (verb) { case POST: conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setDoInput(true); break; case PUT: conn.setRequestMethod("PUT"); conn.setDoOutput(true); break; case DELETE: conn.setRequestMethod("DELETE"); break; default: conn.setRequestMethod("GET"); break; } conn.setRequestProperty("Accept", "application/json"); conn.setRequestProperty("Content-type", "application/json"); return conn; }
From source file:br.bireme.tb.URLS.java
/** * Given an url, loads its content (GET - method) * @param url url to be loaded/*from w w w . j a v a2s . c om*/ * @return an array with the real location of the page (in case of redirect) * and its content. * @throws IOException */ public static String[] loadPageGet(final URL url) throws IOException { if (url == null) { throw new NullPointerException("url"); } System.out.print("loading page (GET) : [" + url + "]"); HttpURLConnection.setFollowRedirects(false); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Accept-Charset", DEFAULT_ENCODING); connection.setRequestProperty("User-Agent", "curl/7.29.0"); connection.setRequestProperty("Accept", "*/*"); connection.connect(); int respCode = connection.getResponseCode(); final StringBuilder builder = new StringBuilder(); String location = url.toString(); while ((respCode >= 300) && (respCode <= 399)) { location = connection.getHeaderField("Location"); connection = (HttpURLConnection) new URL(location).openConnection(); respCode = connection.getResponseCode(); } final boolean respCodeOk = (respCode == 200); final BufferedReader reader; boolean skipLine = false; if (respCodeOk) { reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), DEFAULT_ENCODING)); } else { reader = new BufferedReader(new InputStreamReader(connection.getErrorStream(), DEFAULT_ENCODING)); } while (true) { String line = reader.readLine(); if (line == null) { break; } final String line2 = line.trim(); if (line2.startsWith("<!--")) { if (line2.endsWith("-->")) { continue; } skipLine = true; } else if (line2.endsWith("-->")) { skipLine = false; line = ""; } if (!skipLine) { builder.append(line); builder.append("\n"); } } reader.close(); connection.disconnect(); if (!respCodeOk) { throw new IOException("url=[" + url + "]\ncode=" + respCode + "\n" + builder.toString()); } //System.out.print("+"); System.out.println(" - OK"); return new String[] { location, builder.toString() }; }
From source file:net.mutil.util.HttpUtil.java
public static String connServerForResultPost(String strUrl, HashMap<String, String> entityMap) throws ClientProtocolException, IOException { String strResult = ""; URL url = new URL(HttpUtil.getPCURL() + strUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); StringBuilder entitySb = new StringBuilder(""); Object[] entityKeys = entityMap.keySet().toArray(); for (int i = 0; i < entityKeys.length; i++) { String key = (String) entityKeys[i]; if (i == 0) { entitySb.append(key + "=" + entityMap.get(key)); } else {//from w w w .j a va2 s .c o m entitySb.append("&" + key + "=" + entityMap.get(key)); } } byte[] entity = entitySb.toString().getBytes("UTF-8"); System.out.println(url.toString() + entitySb.toString()); conn.setConnectTimeout(5000); conn.setDoOutput(true); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("Content-Length", String.valueOf(entity.length)); conn.getOutputStream().write(entity); if (conn.getResponseCode() == 200) { InputStream inputstream = conn.getInputStream(); StringBuffer buffer = new StringBuffer(); byte[] b = new byte[4096]; for (int n; (n = inputstream.read(b)) != -1;) { buffer.append(new String(b, 0, n)); } strResult = buffer.toString(); } return strResult; }
From source file:piuk.MyRemoteWallet.java
private static String fetchURL(String URL) throws Exception { if (URL.indexOf("?") > 0) { URL += "&api_code=" + getApiCode(); } else {// w w w .j a v a 2 s. co m URL += "?api_code=" + getApiCode(); } URL url = new URL(URL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); try { connection.setRequestProperty("Accept", "application/json"); connection.setRequestProperty("charset", "utf-8"); connection.setRequestMethod("GET"); connection.setConnectTimeout(180000); connection.setReadTimeout(180000); connection.setInstanceFollowRedirects(false); connection.connect(); if (connection.getResponseCode() == 200) return IOUtils.toString(connection.getInputStream(), "UTF-8"); else if (connection.getResponseCode() == 500) throw new Exception("Error From Server: " + IOUtils.toString(connection.getErrorStream(), "UTF-8")); else throw new Exception("Unknown response from server (" + connection.getResponseCode() + ") " + IOUtils.toString(connection.getErrorStream(), "UTF-8")); } finally { connection.disconnect(); } }