// First set the default cookie manager. CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL)); // All the following subsequent URLConnections will use the same cookie manager. URLConnection connection = new URL(url).openConnection(); // ... connection = new URL(url).openConnection(); // ... connection = new URL(url).openConnection(); // ...
URLConnection connection = new URL(url + "?" + query).openConnection(); connection.setRequestProperty("Accept-Charset", charset); InputStream response = connection.getInputStream(); // ...
HttpURLConnection httpConnection = (HttpURLConnection) new URL(url).openConnection(); httpConnection.setRequestMethod("POST"); // ...
URLConnection connection = new URL(url).openConnection(); connection.setDoOutput(true); // Triggers POST. connection.setRequestProperty("Accept-Charset", charset); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset); try (OutputStream output = connection.getOutputStream()) { output.write(query.getBytes(charset)); } InputStream response = connection.getInputStream(); // ...
String name = c.getString(str_url); URL url_value = new URL(name); ImageView profile = (ImageView)v.findViewById(R.id.vdo_icon); if (profile != null) { Bitmap mIcon1 = BitmapFactory.decodeStream(url_value.openConnection().getInputStream()); profile.setImageBitmap(mIcon1); }
InputStream response = new URL(url).openStream(); // ...
// Gather all cookies on the first request. URLConnection connection = new URL(url).openConnection(); List<String> cookies = connection.getHeaderFields().get("Set-Cookie"); // ... // Then use the same cookies on all subsequent requests. connection = new URL(url).openConnection(); for (String cookie : cookies) { connection.addRequestProperty("Cookie", cookie.split(";", 2)[0]); } // ...
@Override public URI getURI() { try { return this.connection.getURL().toURI(); } catch (URISyntaxException ex) { throw new IllegalStateException("Could not get HttpURLConnection URI: " + ex.getMessage(), ex); } }
/** * Creates a new UDP logging client. * * @param clientName the name of the client to be sent in logging requests. * @param port the port on which client will send logging requests. * @throws UnknownHostException if localhost is unknown */ public UdpLoggingClient(String clientName, int port) throws UnknownHostException { this.clientName = clientName; this.remoteAddress = new InetSocketAddress(InetAddress.getLocalHost(), port); }
public Proxy toProxyAddress() { before(); InetSocketAddress address = new InetSocketAddress(inetSocketAddress.getAddress() .getCanonicalHostName(), getPort()); return new Proxy(Proxy.Type.HTTP, address); }
private InetAddress getConnectToInetAddress(Proxy proxy, HttpUrl url) throws IOException { return proxy.type() != Proxy.Type.DIRECT ? ((InetSocketAddress) proxy.address()).getAddress() : InetAddress.getByName(url.host()); } }
String url = "http://example.com"; String charset = "UTF-8"; // Or in Java 7 and later, use the constant: java.nio.charset.StandardCharsets.UTF_8.name() String param1 = "value1"; String param2 = "value2"; // ... String query = String.format("param1=%s¶m2=%s", URLEncoder.encode(param1, charset), URLEncoder.encode(param2, charset));
@Override protected boolean isPortAvailable(int port) { try { DatagramSocket socket = new DatagramSocket(port, InetAddress.getByName("localhost")); socket.close(); return true; } catch (Exception ex) { return false; } } };
InputStream error = ((HttpURLConnection) connection).getErrorStream();
/** * Extracts the status line from the supplied Java API {@link java.net.HttpURLConnection}. As per * the spec, the status line is held as the header with the null key. Returns {@code null} if * there is no status line. */ private static String extractStatusLine(HttpURLConnection httpUrlConnection) { // Java specifies that this will be be response header with a null key. return httpUrlConnection.getHeaderField(null); }
@Override protected IOException newTimeoutException(IOException cause) { SocketTimeoutException socketTimeoutException = new SocketTimeoutException("timeout"); if (cause != null) { socketTimeoutException.initCause(cause); } return socketTimeoutException; }
@Override public void setDefaultUseCaches(boolean defaultUseCaches) { super.setDefaultUseCaches(defaultUseCaches); } }
@Override public URI getURI() { try { return this.connection.getURL().toURI(); } catch (URISyntaxException ex) { throw new IllegalStateException( "Could not get HttpURLConnection URI: " + ex.getMessage(), ex); } }
@Override public URI getURI() { try { return this.connection.getURL().toURI(); } catch (URISyntaxException ex) { throw new IllegalStateException("Could not get HttpURLConnection URI: " + ex.getMessage(), ex); } }
@Override public URI getURI() { try { return this.connection.getURL().toURI(); } catch (URISyntaxException ex) { throw new IllegalStateException("Could not get HttpURLConnection URI: " + ex.getMessage(), ex); } }