// 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 protected InputStream openObjectStream(URI object, long start) throws IOException { if (supportContentRange) { final URLConnection connection = object.toURL().openConnection(); // Set header for range request. // Since we need to set only the start offset, the header is "bytes=<range-start>-". // See https://tools.ietf.org/html/rfc7233#section-2.1 connection.addRequestProperty(HttpHeaders.RANGE, StringUtils.format("bytes=%d-", start)); return connection.getInputStream(); } else { log.warn( "Since the input source doesn't support range requests, the object input stream is opened from the start and " + "then skipped. This may make the ingestion speed slower. Consider enabling prefetch if you see this message" + " a lot." ); final InputStream in = openObjectStream(object); in.skip(start); return in; } }
URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(10000); conn.setConnectTimeout(15000); conn.addRequestProperty("Content-length", reqEntity.getContentLength()+""); conn.addRequestProperty(reqEntity.getContentType().getName(), reqEntity.getContentType().getValue()); return readStream(conn.getInputStream());
OkHttpClient client = new OkHttpClient(); OutputStream out = null; try { URL url = new URL("http://www.example.com"); HttpURLConnection connection = client.open(url); for (Map.Entry<String, String> entry : multipart.getHeaders().entrySet()) { connection.addRequestProperty(entry.getKey(), entry.getValue()); } connection.setRequestMethod("POST"); // Write the request. out = connection.getOutputStream(); multipart.writeBodyTo(out); out.close(); // Read the response. if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { throw new IOException("Unexpected HTTP response: " + connection.getResponseCode() + " " + connection.getResponseMessage()); } } finally { // Clean up. try { if (out != null) out.close(); } catch (Exception e) { } }
private URLConnection openConnection(URL finalURL) throws IOException { URLConnection connection = finalURL.openConnection(); final boolean http = connection instanceof HttpURLConnection; if (http && tryGzip) { connection.addRequestProperty("Accept-Encoding", "gzip"); } // mind, connect timeout is in seconds if (http && getConnectTimeout() > 0) { connection.setConnectTimeout(1000 * getConnectTimeout()); } if (http && getReadTimeout() > 0) { connection.setReadTimeout(1000 * getReadTimeout()); } final String username = getUser(); final String password = getPassword(); if (http && username != null && password != null) { String userpassword = username + ":" + password; String encodedAuthorization = Base64.encodeBytes(userpassword.getBytes("UTF-8"), Base64.DONT_BREAK_LINES); connection.setRequestProperty("Authorization", "Basic " + encodedAuthorization); } return connection; }
private InputStream read() { try { HttpURLConnection httpcon = (HttpURLConnection) url.openConnection(); httpcon.addRequestProperty("User-Agent", "Mozilla/4.0"); return httpcon.getInputStream(); } catch (IOException e) { String error = e.toString(); throw new RuntimeException(e); } }
conn = url.openConnection(); conn.addRequestProperty("User-Agent", USER_AGENT); conn.addRequestProperty("Accept", "application/xml, text/xml, */*"); if (conn instanceof HttpURLConnection) else url = new URL(newLocation); count ++; stream = conn.getInputStream(); return parse( stream, type, options );
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());
final URLConnection connection = url.openConnection(); connection.addRequestProperty("Accept", accept != null ? accept : parseAsText ? "text/plain,text/*" : "application/xml,text/xml,application/*+xml,text/*+xml"); if (acceptLanguage != null) connection.addRequestProperty("Accept-Language", acceptLanguage); inputStream = connection.getInputStream(); try { if (parseAsText) {
@Override public Result authenticate(HttpExchange pHttpExchange) { try { URLConnection connection = delegateURL.openConnection(); connection.addRequestProperty("Authorization", pHttpExchange.getRequestHeaders().getFirst("Authorization")); connection.setConnectTimeout(2000); connection.connect(); if (connection instanceof HttpURLConnection) { HttpURLConnection httpConnection = (HttpURLConnection) connection; return httpConnection.getResponseCode() == 200 ? new Success(principalExtractor.extract(connection)) : new Failure(401); } else { return new Failure(401); } } catch (final IOException e) { return prepareFailure(pHttpExchange, "Cannot call delegate url " + delegateURL + ": " + e, 503); } catch (final IllegalArgumentException e) { return prepareFailure(pHttpExchange, "Illegal Argument: " + e, 400); } catch (ParseException e) { return prepareFailure(pHttpExchange, "Invalid JSON response: " + e, 422); } }
try { HttpURLConnection connection = ((HttpURLConnection)url.openConnection()); connection.addRequestProperty("User-Agent", "Mozilla/4.0"); InputStream input; if (connection.getResponseCode() == 200) // this must be called before 'getErrorStream()' works input = connection.getInputStream(); else input = connection.getErrorStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); String msg; while ((msg =reader.readLine()) != null) System.out.println(msg); } catch (IOException e) { System.err.println(e); }
try { url = new URL("https://www.google.com/accounts/ClientLogin"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("POST"); urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); url = new URL(googleReaderTokenUrl); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); Log.d("GoogleReader", "Auth.b=" + authorization); urlConnection.addRequestProperty("Authorization", "GoogleLogin auth=" + authorization); urlConnection.setRequestMethod("GET"); urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlendcoded"); InputStream inputStream = new BufferedInputStream(urlConnection.getInputStream()); responseString = convertStreamToString(inputStream); }catch(Exception e){ url = new URL(googleAuthUrl); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("POST"); Log.d("GoogleReader", "Auth.c=" + authorization); urlConnection.addRequestProperty("Authorization", "GoogleLogin auth=" + authorization); urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); urlConnection.setRequestProperty("Content-Length", Integer.toString(sb.toString().getBytes("UTF-8").length));
@Override public void connect(DownloadRequest request) throws IOException { connection = new URL(request.getUrl()).openConnection(); connection.setReadTimeout(request.getReadTimeout()); connection.setConnectTimeout(request.getConnectTimeout()); final String range = String.format(Locale.ENGLISH, "bytes=%d-", request.getDownloadedBytes()); connection.addRequestProperty(Constants.RANGE, range); connection.addRequestProperty(Constants.USER_AGENT, request.getUserAgent()); addHeaders(request); connection.connect(); }
public static String getHTML(URL url) { try { final URLConnection urlConnection = url.openConnection(); urlConnection.addRequestProperty("User-Agent", "Foo?"); final InputStream inputStream = urlConnection.getInputStream(); final String html = IOUtils.toString(inputStream); inputStream.close(); return html; } catch (Exception e) { throw new RuntimeException(e); }
URI uri = new URI("the server address goes here"); HttpURLConnection conn = (HttpURLConnection) uri.toURL().openConnection(); conn.setDoOutput(true); conn.setRequestMethod("PUT"); conn.addRequestProperty("Content-Type", "application/json"); OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream()); out.write(gson.toJson(newClient)); out.close(); // Check here that you succeeded!
URL url = new URL( schemaLocation ); URLConnection conn = url.openConnection(); conn.addRequestProperty("User-Agent", USER_AGENT); conn.addRequestProperty("Accept", "application/xml, text/xml, */*"); DigestInputStream input = digestInputStream(conn.getInputStream()); IOUtil.copyCompletely(input, buffer); digest = HexBin.bytesToString(input.getMessageDigest().digest());
private URLConnection openConnection(final URL aSource) throws IOException { final URLConnection connection = aSource.openConnection(); connection.addRequestProperty("User-Agent", this.userAgent); : "") + " moved to " + newLocation; log(message, logLevel); final URL newURL = new URL(aSource, newLocation); if (!redirectionAllowed(aSource, newURL)) { return null;
private static InputStream openGZIPInputStream(URL url, String auth, int timeout) throws IOException { final URLConnection connection = url.openConnection(); connection.setConnectTimeout(CONNECTION_TIMEOUT); connection.setReadTimeout(timeout); connection.addRequestProperty(GZIP_HTTP_HEADER, GZIP_COMPRESSION); if (null != auth) { connection.setRequestProperty("Authorization", auth); } return new GZIPInputStream(connection.getInputStream()); }
protected static URLConnection rebuildConnection(OWLOntologyLoaderConfiguration config, int connectionTimeout, URL newURL, String acceptHeaders) throws IOException { URLConnection conn; conn = newURL.openConnection(); conn.addRequestProperty("Accept", acceptHeaders); if (config.isAcceptingHTTPCompression()) { conn.setRequestProperty("Accept-Encoding", ACCEPTABLE_CONTENT_ENCODING); } conn.setConnectTimeout(connectionTimeout); return conn; }
public static void main(String[] args) throws Exception { URL whatismyip = new URL("http://automation.whatismyip.com/n09230945.asp"); URLConnection connection = whatismyip.openConnection(); connection.addRequestProperty("Protocol", "Http/1.1"); connection.addRequestProperty("Connection", "keep-alive"); connection.addRequestProperty("Keep-Alive", "1000"); connection.addRequestProperty("User-Agent", "Web-Agent"); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String ip = in.readLine(); //you get the IP as a String System.out.println(ip); }