HttpURLConnection httpUrlConnection = null; URL url = new URL("http://example.com/server.cgi"); httpUrlConnection = (HttpURLConnection) url.openConnection(); httpUrlConnection.setUseCaches(false); httpUrlConnection.setDoOutput(true); httpUrlConnection.setRequestMethod("POST"); httpUrlConnection.setRequestProperty("Connection", "Keep-Alive"); httpUrlConnection.setRequestProperty("Cache-Control", "no-cache"); httpUrlConnection.setRequestProperty( "Content-Type", "multipart/form-data;boundary=" + this.boundary);
public Document parse(final DocumentBuilder parser) throws SAXException, IOException { URLConnection uConn = url.openConnection(); uConn.setUseCaches(false); InputStream stream = uConn.getInputStream(); try { InputSource src = new InputSource(stream); src.setSystemId(url.toString()); return parser.parse(src); } finally { stream.close(); } } public String toString() {
URL url = new URL(strUrl); URLConnection connection = url.openConnection(); connection.setUseCaches(true); Object response = connection.getContent(); if (response instanceof Bitmap) { Bitmap bitmap = (Bitmap)response; }
final URLConnection connection = url.openConnection(); if (parameters != null) { if (parameters.containsKey("connectTimeout")) { connection.setUseCaches(DefaultGroovyMethods.asType(parameters.get("useCaches"), Boolean.class)); Map<String, CharSequence> properties = (Map<String, CharSequence>) parameters.get("requestProperties"); for (Map.Entry<String, CharSequence> entry : properties.entrySet()) { connection.setRequestProperty(entry.getKey(), entry.getValue().toString()); return connection.getInputStream();
/** * @param useCaches {@code null} if this aspect wasn't set in the parent {@link TemplateLoader}. */ URLTemplateSource(URL url, Boolean useCaches) throws IOException { this.url = url; this.conn = url.openConnection(); this.useCaches = useCaches; if (useCaches != null) { conn.setUseCaches(useCaches.booleanValue()); } }
/** * Ouvre la connection http. * @return Object * @throws IOException Exception de communication */ private URLConnection openConnection() throws IOException { final URLConnection connection = url.openConnection(); connection.setUseCaches(false); if (CONNECTION_TIMEOUT > 0) { connection.setConnectTimeout(CONNECTION_TIMEOUT); } if (READ_TIMEOUT > 0) { connection.setReadTimeout(READ_TIMEOUT); } // grâce à cette propriété, l'application retournera un flux compressé si la taille // dépasse x Ko connection.setRequestProperty("Accept-Encoding", "gzip"); if (headers != null) { for (final Map.Entry<String, String> entry : headers.entrySet()) { connection.setRequestProperty(entry.getKey(), entry.getValue()); } } if (url.getUserInfo() != null) { final String authorization = Base64Coder.encodeString(url.getUserInfo()); connection.setRequestProperty("Authorization", "Basic " + authorization); } return connection; }
protected void writeCall(String methodName, Object[] args) throws IOException { huc = u.openConnection(); huc.setDoOutput(true); huc.setDoInput(true); huc.setUseCaches(false); huc.setRequestProperty("Content-Type", "binary/message-pack"); huc.setReadTimeout(0); OutputStream os = huc.getOutputStream(); Packer pk = msgpack.createPacker(os); pk.writeArrayBegin(args.length+1); pk.write(methodName); for (Object arg : args) pk.write(arg); pk.writeArrayEnd(); pk.close(); os.close(); }
System.setProperty("http.keepAlive", "false"); HttpURLConnection conn = (HttpURLConnection) mURL.openConnection(); conn.setUseCaches(false); conn.setRequestProperty("User-Agent", useragent); conn.setConnectTimeout(30000); conn.setDoOutput(true); conn.setDoInput(true); consumer.sign(conn); conn.connect(); InputSource is = new InputSource(conn.getInputStream());
String urlParameters = "param1=a¶m2=b¶m3=c"; byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 ); int postDataLength = postData.length; String request = "http://example.com/index.php"; URL url = new URL( request ); HttpURLConnection conn= (HttpURLConnection) url.openConnection(); conn.setDoOutput( true ); conn.setInstanceFollowRedirects( false ); conn.setRequestMethod( "POST" ); conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty( "charset", "utf-8"); conn.setRequestProperty( "Content-Length", Integer.toString( postDataLength )); conn.setUseCaches( false ); try( DataOutputStream wr = new DataOutputStream( conn.getOutputStream())) { wr.write( postData ); }
/** * Opens a resource of the specified name for reading. Controls caching, * that is important when the same jar is reloaded using custom classloader. */ public static InputStream getResourceAsStream(String resourceName, ClassLoader callingClass, boolean useCache) throws IOException { URL url = getResourceUrl(resourceName, callingClass); if (url != null) { URLConnection urlConnection = url.openConnection(); urlConnection.setUseCaches(useCache); return urlConnection.getInputStream(); } return null; }
String addr = "http://172.26.41.18:8080/domain/list"; URL url = new URL(addr); HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); httpCon.setUseCaches(false); httpCon.setAllowUserInteraction(false); httpCon.addRequestProperty("Authorization", "Basic YWRtaW4fYFgjkl5463"); System.out.println(httpCon.getResponseCode()); System.out.println(httpCon.getResponseMessage());
public Dir createDir(URL url) throws Exception { try { URLConnection urlConnection = url.openConnection(); if (urlConnection instanceof JarURLConnection) { urlConnection.setUseCaches(false); return new ZipDir(((JarURLConnection) urlConnection).getJarFile()); } } catch (Throwable e) { /*fallback*/ } java.io.File file = getFile(url); if (file != null) { return new ZipDir(new JarFile(file)); } return null; } },
/** * Downloads the contents of a URL and copies them to a string (Borrowed from oreilly) * * @param url * @return InputStream of contents * @should return a valid input stream for old module urls */ public static InputStream getURLStream(URL url) { InputStream in = null; try { URLConnection uc = url.openConnection(); uc.setDefaultUseCaches(false); uc.setUseCaches(false); uc.setRequestProperty("Cache-Control", "max-age=0,no-cache"); uc.setRequestProperty("Pragma", "no-cache"); log.debug("Logging an attempt to connect to: " + url); in = openConnectionCheckRedirects(uc); } catch (IOException io) { log.warn("io while reading: " + url, io); } return in; }
URL myURL = new URL(serviceURL); HttpURLConnection myURLConnection = (HttpURLConnection)myURL.openConnection(); String userCredentials = "username:password"; String basicAuth = "Basic " + new String(new Base64().encode(userCredentials.getBytes())); myURLConnection.setRequestProperty ("Authorization", basicAuth); myURLConnection.setRequestMethod("POST"); myURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); myURLConnection.setRequestProperty("Content-Length", "" + postData.getBytes().length); myURLConnection.setRequestProperty("Content-Language", "en-US"); myURLConnection.setUseCaches(false); myURLConnection.setDoInput(true); myURLConnection.setDoOutput(true);
/** * Opens an {@link InputStream} reading from the given URL without * caching the stream. This prevents file descriptor leaks when reading * from file system URLs. * * @param url the URL to connect to * @return an input stream reading from the URL connection */ public static InputStream openUncachedStream(URL url) throws IOException { URLConnection urlConnection = url.openConnection(); urlConnection.setUseCaches(false); return urlConnection.getInputStream(); } }
/** * When URL Connection uses cache, it may keep file handler. This method open connection without caching to avoid * file handler leak. * * @return opened {@link URLConnection} which does not use cache to load data * @see <a href="https://github.com/spotbugs/spotbugs/issues/589">related GitHub issue</a> */ @CheckReturnValue @NonNull public static URLConnection openNonCachedConnection(@NonNull URL u) throws IOException { URLConnection uc = u.openConnection(); if (uc instanceof JarURLConnection) { uc.setUseCaches(false); } return uc; }
URL url = new URL(targetURL); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("Content-Length", Integer.toString(urlParameters.getBytes().length)); connection.setRequestProperty("Content-Language", "en-US"); connection.setUseCaches(false); connection.setDoOutput(true); connection.getOutputStream()); wr.writeBytes(urlParameters); wr.close(); InputStream is = connection.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is)); StringBuilder response = new StringBuilder(); // or StringBuffer if Java version 5+
public static String file2String(final URL url) { InputStream in = null; try { URLConnection urlConnection = url.openConnection(); urlConnection.setUseCaches(false); in = urlConnection.getInputStream(); int len = in.available(); byte[] data = new byte[len]; in.read(data, 0, len); return new String(data, "UTF-8"); } catch (Exception ignored) { } finally { if (null != in) { try { in.close(); } catch (IOException ignored) { } } } return null; }
URL url; URLConnection urlConn; DataOutputStream printout; DataInputStream input; url = new URL (getCodeBase().toString() + "env.tcgi"); urlConn = url.openConnection(); urlConn.setDoInput (true); urlConn.setDoOutput (true); urlConn.setUseCaches (false); urlConn.setRequestProperty("Content-Type","application/json"); urlConn.setRequestProperty("Host", "android.schoolportal.gr"); urlConn.connect(); //Create JSONObject here JSONObject jsonParam = new JSONObject(); jsonParam.put("ID", "25"); jsonParam.put("description", "Real"); jsonParam.put("enable", "true");
@Nonnull @Override public InputStream asInputStream() throws IOException { URLConnection con = this.url.openConnection(); con.setUseCaches(true); try { return con.getInputStream(); } catch (IOException ex) { if (con instanceof HttpURLConnection) { ((HttpURLConnection) con).disconnect(); } throw ex; } }