List of usage examples for java.net URLConnection setRequestProperty
public void setRequestProperty(String key, String value)
From source file:opennlp.tools.similarity.apps.WebSearchEngineResultsScraper.java
protected static String fetchPageSearchEngine(String url) { System.out.println("fetch url " + url); String pageContent = null;/*www . jav a 2 s .co m*/ StringBuffer buf = new StringBuffer(); try { URLConnection connection = new URL(url).openConnection(); connection.setReadTimeout(50000); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3"); String line; BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); } catch (Exception e) { System.err.println("Unable to complete search engine request " + url); //e.printStackTrace(); } while ((line = reader.readLine()) != null) { buf.append(line); } } catch (Exception e) { // e.printStackTrace(); System.err.println("error fetching url " + url); } return buf.toString(); }
From source file:eionet.cr.util.FileUtil.java
/** * Downloads the contents of the given URL into the given file and closes the file. * * @param urlString downloadable url//from w w w . ja v a 2 s . c o m * @param toFile output file location * @throws IOException if input of output fails */ public static void downloadUrlToFile(final String urlString, final File toFile) throws IOException { URLConnection urlConnection = null; InputStream inputStream = null; try { URL url = new URL(urlString == null ? urlString : StringUtils.replace(urlString, " ", "%20")); urlConnection = url.openConnection(); urlConnection.setRequestProperty("Connection", "close"); inputStream = urlConnection.getInputStream(); FileUtil.streamToFile(inputStream, toFile); } finally { IOUtils.closeQuietly(inputStream); URLUtil.disconnect(urlConnection); } }
From source file:org.connectbot.util.UpdateHelper.java
/** * Read contents of a URL and return as a String. Handles any server * downtime with a 6-second timeout.// www . j ava 2s .c om */ private static String getUrl(String tryUrl, String userAgent) throws Exception { URL url = new URL(tryUrl); URLConnection connection = url.openConnection(); connection.setConnectTimeout(6000); connection.setReadTimeout(6000); connection.setRequestProperty("User-Agent", userAgent); connection.connect(); InputStream is = connection.getInputStream(); ByteArrayOutputStream os = new ByteArrayOutputStream(); int bytesRead; byte[] buffer = new byte[1024]; while ((bytesRead = is.read(buffer)) != -1) { os.write(buffer, 0, bytesRead); } os.flush(); os.close(); is.close(); return new String(os.toByteArray()); }
From source file:Main.java
/** * This method is used to send a XML request file to web server to process the request and return * xml response containing event id./* w ww . j a v a 2 s . c o m*/ */ public static String postXMLWithTimeout(String postUrl, String xml, int readTimeout) throws Exception { System.out.println("XMLUtils.postXMLWithTimeout: Connecting to Web Server ......."); InputStream in = null; BufferedReader bufferedReader = null; OutputStream out = null; PrintWriter printWriter = null; StringBuffer responseMessageBuffer = new StringBuffer(""); try { URL url = new URL(postUrl); URLConnection con = url.openConnection(); // Prepare for both input and output con.setDoInput(true); con.setDoOutput(true); // Turn off caching con.setUseCaches(false); con.setRequestProperty("Content-Type", "text/xml"); con.setReadTimeout(readTimeout); out = con.getOutputStream(); // Write the arguments as post data printWriter = new PrintWriter(out); printWriter.println(xml); printWriter.flush(); //Process response and return back to caller function in = con.getInputStream(); bufferedReader = new BufferedReader(new InputStreamReader(in)); String tempStr = null; int tempClearResponseMessageBufferCounter = 0; while ((tempStr = bufferedReader.readLine()) != null) { tempClearResponseMessageBufferCounter++; //clear the buffer for the first time if (tempClearResponseMessageBufferCounter == 1) responseMessageBuffer.setLength(0); responseMessageBuffer.append(tempStr); } } catch (Exception ex) { throw ex; } finally { System.out.println("Calling finally in POSTXML"); try { if (in != null) in.close(); } catch (Exception eex) { System.out.println("COULD NOT Close Input Stream in try"); } try { if (out != null) out.close(); } catch (Exception eex) { System.out.println("COULD NOT Close Output Stream in try"); } } System.out.println("XMLUtils.postXMLWithTimeout: end ......."); return responseMessageBuffer.toString(); }
From source file:dictinsight.utils.io.HttpUtils.java
public static String getDataFromOtherServer(String url, String param) { PrintWriter out = null;//from w ww. j a v a2 s .c o m BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); URLConnection conn = realUrl.openConnection(); conn.setConnectTimeout(1000 * 10); conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); conn.setDoOutput(true); conn.setDoInput(true); out = new PrintWriter(conn.getOutputStream()); out.print(param); out.flush(); in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("get date from " + url + param + " error!"); e.printStackTrace(); } // finally????? finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return result; }
From source file:org.alfresco.webservice.util.ContentUtils.java
/** * Get the content from the download servlet as a string * //from w w w .ja v a 2s . co m * @param content the content object * @return the content as a string */ public static String getContentAsString(Content content) { // Get the url and the ticket String ticket = AuthenticationUtils.getTicket(); String strUrl = content.getUrl() + "?ticket=" + ticket; StringBuilder readContent = new StringBuilder(); try { // Connect to donwload servlet URL url = new URL(strUrl); URLConnection conn = url.openConnection(); conn.setRequestProperty("Cookie", "JSESSIONID=" + AuthenticationUtils.getAuthenticationDetails().getSessionId() + ";"); InputStream is = conn.getInputStream(); int read = is.read(); while (read != -1) { readContent.append((char) read); read = is.read(); } } catch (Exception exception) { throw new WebServiceException("Unable to get content as string.", exception); } // return content as a string return readContent.toString(); }
From source file:com.moviejukebox.themoviedb.tools.WebBrowser.java
public static URLConnection openProxiedConnection(URL url) throws MovieDbException { try {/*from w w w . ja v a2 s . c o m*/ if (proxyHost != null) { System.getProperties().put("proxySet", "true"); System.getProperties().put("proxyHost", proxyHost); System.getProperties().put("proxyPort", proxyPort); } URLConnection cnx = url.openConnection(); if (proxyUsername != null) { cnx.setRequestProperty("Proxy-Authorization", proxyEncodedPassword); } return cnx; } catch (IOException ex) { throw new MovieDbException(MovieDbException.MovieDbExceptionType.INVALID_URL, null, ex); } }
From source file:eionet.cr.util.xml.ConversionsParser.java
/** * * @param schemaUri/* www. j av a 2s .c o m*/ * @return * @throws IOException * @throws ParserConfigurationException * @throws SAXException */ public static ConversionsParser parseForSchema(String schemaUri) throws IOException, SAXException, ParserConfigurationException { String listConversionsUrl = GeneralConfig.getRequiredProperty(GeneralConfig.XMLCONV_LIST_CONVERSIONS_URL); listConversionsUrl = MessageFormat.format(listConversionsUrl, Util.toArray(URLEncoder.encode(schemaUri))); URL url = new URL(listConversionsUrl); URLConnection urlConnection = url.openConnection(); InputStream inputStream = null; try { // avoid keep-alive by setting "Connection: close" header urlConnection.setRequestProperty("Connection", "close"); inputStream = urlConnection.getInputStream(); ConversionsParser conversionsParser = new ConversionsParser(); conversionsParser.parse(inputStream); return conversionsParser; } finally { IOUtils.closeQuietly(inputStream); URLUtil.disconnect(urlConnection); } }
From source file:org.jenkins_ci.update_center.repo.NexusRepositoryImpl.java
/** * Loads a remote repository index (.zip or .gz), convert it to Lucene index and return it. *//*from w w w . j a v a 2 s .c om*/ private static File loadIndex(String id, URL url) throws IOException, UnsupportedExistingLuceneIndexException { File dir = new File(new File(System.getProperty("java.io.tmpdir")), "maven-index/" + id); File local = new File(dir, "index" + getExtension(url)); File expanded = new File(dir, "expanded"); URLConnection con = url.openConnection(); if (url.getUserInfo() != null) { con.setRequestProperty("Authorization", "Basic " + new sun.misc.BASE64Encoder().encode(url.getUserInfo().getBytes())); } if (!expanded.exists() || !local.exists() || local.lastModified() != con.getLastModified()) { System.out.println("Downloading " + url); // if the download fail in the middle, only leave a broken tmp file dir.mkdirs(); File tmp = new File(dir, "index_" + getExtension(url)); FileOutputStream o = new FileOutputStream(tmp); try { IOUtils.copy(con.getInputStream(), o); } finally { o.close(); } if (expanded.exists()) { FileUtils.deleteDirectory(expanded); } expanded.mkdirs(); if (url.toExternalForm().endsWith(".gz")) { FSDirectory directory = FSDirectory.getDirectory(expanded); NexusIndexWriter w = new NexusIndexWriter(directory, new NexusAnalyzer(), true); FileInputStream in = new FileInputStream(tmp); try { IndexDataReader dr = new IndexDataReader(in); dr.readIndex(w, new DefaultIndexingContext(id, id, null, expanded, null, null, NexusIndexer.DEFAULT_INDEX, true)); } finally { IndexUtils.close(w); IOUtils.closeQuietly(in); directory.close(); } } else if (url.toExternalForm().endsWith(".zip")) { Expand e = new Expand(); e.setSrc(tmp); e.setDest(expanded); e.execute(); } else { throw new UnsupportedOperationException("Unsupported index format: " + url); } // as a proof that the expansion was properly completed tmp.renameTo(local); local.setLastModified(con.getLastModified()); } else { System.out.println("Reusing the locally cached " + url + " at " + local); } return expanded; }
From source file:fr.free.divde.webcam.WebcamApplet.java
private static void setHeaders(URLConnection urlConnection, Map<String, Object> headers) { for (Entry<String, Object> entry : headers.entrySet()) { urlConnection.setRequestProperty(entry.getKey().toLowerCase(), entry.getValue().toString()); }/*from w w w . j a va 2 s . c om*/ }