List of usage examples for java.net URLConnection setRequestProperty
public void setRequestProperty(String key, String value)
From source file:no.get.cms.plugin.resourcecompressor.ContentLoader.java
public String load(String srcUrl) { try {//from w ww . j a va2 s . c o m URL url = new URL(srcUrl); URLConnection connection = url.openConnection(); connection.setRequestProperty("Accept-Charset", "UTF-8"); connection.setConnectTimeout(2000); connection.setReadTimeout(10000); connection.connect(); InputStream inputStream = connection.getInputStream(); String charset = extractCharsetFromContentType(connection.getContentType()); String body = IOUtils.toString(inputStream, charset); IOUtils.close(connection); return body; } catch (MalformedURLException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:org.soulwing.cas.client.UrlProtocolSource.java
public InputSource getSource(String url) { try {// ww w .ja v a2 s . c o m log.debug("requesting " + url); URLConnection connection = getURL(url).openConnection(); connection.setRequestProperty("Connection", "close"); return new InputSource(connection.getInputStream()); } catch (IOException ex) { throw new ServiceAccessException(ex); } }
From source file:CounterApp.java
public int getCount() throws Exception { java.net.URL url = new java.net.URL(servletURL); java.net.URLConnection con = url.openConnection(); if (sessionCookie != null) { con.setRequestProperty("cookie", sessionCookie); }//w w w.j av a 2 s . c o m con.setUseCaches(false); con.setDoOutput(true); con.setDoInput(true); ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(byteOut); out.flush(); byte buf[] = byteOut.toByteArray(); con.setRequestProperty("Content-type", "application/octet-stream"); con.setRequestProperty("Content-length", "" + buf.length); DataOutputStream dataOut = new DataOutputStream(con.getOutputStream()); dataOut.write(buf); dataOut.flush(); dataOut.close(); DataInputStream in = new DataInputStream(con.getInputStream()); int count = in.readInt(); in.close(); if (sessionCookie == null) { String cookie = con.getHeaderField("set-cookie"); if (cookie != null) { sessionCookie = parseCookie(cookie); System.out.println("Setting session ID=" + sessionCookie); } } return count; }
From source file:util.io.IOUtilities.java
/** * Originally copied from javax.swing.JEditorPane. * <p>/* w w w . j a v a 2 s .co m*/ * Fetches a stream for the given URL, which is about to * be loaded by the <code>setPage</code> method. By * default, this simply opens the URL and returns the * stream. This can be reimplemented to do useful things * like fetch the stream from a cache, monitor the progress * of the stream, etc. * <p> * This method is expected to have the the side effect of * establishing the content type, and therefore setting the * appropriate <code>EditorKit</code> to use for loading the stream. * <p> * If this the stream was an http connection, redirects * will be followed and the resulting URL will be set as * the <code>Document.StreamDescriptionProperty</code> so that relative * URL's can be properly resolved. * * @param page the URL of the page * @param followRedirects Follow redirects. * @param timeout The read timeout. * @param userName The user name to use for the connection. * @param userPassword The password to use for the connection. * @throws IOException if something went wrong. * @return a stream reading data from the specified URL. */ public static InputStream getStream(URL page, boolean followRedirects, int timeout, String userName, String userPassword) throws IOException { URLConnection conn = page.openConnection(); if (userName != null && userPassword != null) { String password = userName + ":" + userPassword; String encodedPassword = new String(Base64.encodeBase64(password.getBytes())); conn.setRequestProperty("Authorization", "Basic " + encodedPassword); } if (timeout > 0) { conn.setReadTimeout(timeout); } if (followRedirects && (conn instanceof HttpURLConnection)) { HttpURLConnection hconn = (HttpURLConnection) conn; hconn.setInstanceFollowRedirects(false); int response = hconn.getResponseCode(); boolean redirect = (response >= 300 && response <= 399); // In the case of a redirect, we want to actually change the URL // that was input to the new, redirected URL if (redirect) { String loc = conn.getHeaderField("Location"); if (loc == null) { throw new FileNotFoundException( "URL points to a redirect without " + "target location: " + page); } if (loc.startsWith("http")) { page = new URL(loc); } else { page = new URL(page, loc); } return getStream(page, followRedirects, timeout, userName, userPassword); } } InputStream in = conn.getInputStream(); return in; }
From source file:net.bashtech.geobot.BotManager.java
public static String getRemoteContent(String urlString) { String dataIn = ""; try {// www . j ava 2 s .c o m URL url = new URL(urlString); // System.out.println("DEBUG: Getting data from " + url.toString()); URLConnection conn = url.openConnection(); conn.setRequestProperty("User-Agent", "CoeBot"); // conn.setConnectTimeout(5 * 1000); // conn.setReadTimeout(5 * 1000); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) dataIn += inputLine; in.close(); } catch (Exception ex) { if (ex instanceof SocketTimeoutException) { return "API took too long to respond."; } ex.printStackTrace(); } return dataIn; }
From source file:com.github.bfour.fpliteraturecollector.service.FileStorageService.java
public Link persist(URL webAddress, Literature lit) throws IOException { String fileName = getFileNameForLiterature(lit); fileName += ".pdf"; // + FilenameUtils.getExtension(webAddress.getFile()).substring(0, // 3);//from w w w . ja v a 2 s .c om File file = new File(rootDirectory.getAbsolutePath() + "/" + lit.getID() + "/" + fileName); URLConnection conn = webAddress.openConnection(); conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0"); conn.connect(); FileUtils.copyInputStreamToFile(conn.getInputStream(), file); return new Link(fileName, file.toURI(), webAddress.toString()); }
From source file:fr.jetoile.hadoopunit.component.HdfsBootstrapTest.java
@Test public void hdfsShouldStart() throws Exception { Assertions.assertThat(Utils.available("127.0.0.1", 20112)).isFalse(); // Write a file to HDFS containing the test string FileSystem hdfsFsHandle = ((HdfsBootstrap) HadoopBootstrap.INSTANCE.getService(Component.HDFS)) .getHdfsFileSystemHandle();/*from ww w.j a v a 2 s.c o m*/ FSDataOutputStream writer = hdfsFsHandle .create(new Path(configuration.getString(HadoopUnitConfig.HDFS_TEST_FILE_KEY))); writer.writeUTF(configuration.getString(HadoopUnitConfig.HDFS_TEST_STRING_KEY)); writer.close(); // Read the file and compare to test string FSDataInputStream reader = hdfsFsHandle .open(new Path(configuration.getString(HadoopUnitConfig.HDFS_TEST_FILE_KEY))); assertEquals(reader.readUTF(), configuration.getString(HadoopUnitConfig.HDFS_TEST_STRING_KEY)); reader.close(); hdfsFsHandle.close(); URL url = new URL(String.format("http://localhost:%s/webhdfs/v1?op=GETHOMEDIRECTORY&user.name=guest", configuration.getInt(HadoopUnitConfig.HDFS_NAMENODE_HTTP_PORT_KEY))); URLConnection connection = url.openConnection(); connection.setRequestProperty("Accept-Charset", "UTF-8"); BufferedReader response = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line = response.readLine(); response.close(); assertThat("{\"Path\":\"/user/guest\"}").isEqualTo(line); }
From source file:org.gradle.api.internal.resource.UriResource.java
private InputStream getInputStream(URI url) throws IOException { final URLConnection urlConnection = url.toURL().openConnection(); urlConnection.setRequestProperty("User-Agent", getUserAgentString()); return urlConnection.getInputStream(); }
From source file:com.github.sakserv.minicluster.impl.HdfsLocalClusterIntegrationTest.java
@Test public void testDfsClusterStart() throws Exception { // Write a file to HDFS containing the test string FileSystem hdfsFsHandle = dfsCluster.getHdfsFileSystemHandle(); FSDataOutputStream writer = hdfsFsHandle .create(new Path(propertyParser.getProperty(ConfigVars.HDFS_TEST_FILE_KEY))); writer.writeUTF(propertyParser.getProperty(ConfigVars.HDFS_TEST_STRING_KEY)); writer.close();//from w w w . j av a 2 s.c om // Read the file and compare to test string FSDataInputStream reader = hdfsFsHandle .open(new Path(propertyParser.getProperty(ConfigVars.HDFS_TEST_FILE_KEY))); assertEquals(reader.readUTF(), propertyParser.getProperty(ConfigVars.HDFS_TEST_STRING_KEY)); reader.close(); hdfsFsHandle.close(); URL url = new URL(String.format("http://localhost:%s/webhdfs/v1?op=GETHOMEDIRECTORY&user.name=guest", propertyParser.getProperty(ConfigVars.HDFS_NAMENODE_HTTP_PORT_KEY))); URLConnection connection = url.openConnection(); connection.setRequestProperty("Accept-Charset", "UTF-8"); BufferedReader response = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line = response.readLine(); response.close(); assertEquals("{\"Path\":\"/user/guest\"}", line); }
From source file:webcamrequest.WebCamRequest.java
public double getWind() throws MalformedURLException, IOException { System.out.println("*** START **"); try {/*from www . ja v a2 s .c om*/ String authString = name + ":" + password; System.out.println("auth string: " + authString); byte[] authEncBytes = Base64.encodeBase64(authString.getBytes()); String authStringEnc = new String(authEncBytes); System.out.println("Base64 encoded auth string: " + authStringEnc); URL url = new URL(webPage); URLConnection urlConnection = url.openConnection(); urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc); InputStream is = urlConnection.getInputStream(); // volstndiges bild ByteArrayOutputStream buffer = new ByteArrayOutputStream(); int nRead; byte[] data = new byte[1024]; while ((nRead = is.read(data, 0, data.length)) != -1) { buffer.write(data, 0, nRead); } buffer.flush(); byte[] byteArray = buffer.toByteArray(); sendBild(byteArray); // so bild oft unvollstndig // byte[] buffer = new byte[is.available()]; // is.read(buffer); // sendBild(buffer); // File targetFile = new File("d:/bild.jpg"); // OutputStream outStream = new FileOutputStream(targetFile); // outStream.write(buffer); System.out.println("*** END ***"); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return 0; }