List of usage examples for java.net HttpURLConnection setInstanceFollowRedirects
public void setInstanceFollowRedirects(boolean followRedirects)
From source file:com.google.ytd.picasa.PicasaApiHelper.java
public PhotoEntry doResumableUpload(com.google.ytd.model.PhotoEntry photoEntry) throws IllegalArgumentException { if (util.isNullOrEmpty(photoEntry.getResumableUploadUrl())) { throw new IllegalArgumentException(String .format("No resumable upload URL found for " + "PhotoEntry id '%s'.", photoEntry.getId())); }/*w ww . j a v a2s . co m*/ try { URL url = new URL(photoEntry.getResumableUploadUrl()); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setInstanceFollowRedirects(false); connection.setConnectTimeout(CONNECT_TIMEOUT); connection.setReadTimeout(READ_TIMEOUT); connection.setRequestMethod("PUT"); connection.setRequestProperty("Content-Range", "bytes */*"); // Response code 308 is specific to this use case and doesn't appear to have a // HttpURLConnection constant. if (connection.getResponseCode() == 308) { long previousByte = 0; String rangeHeader = connection.getHeaderField("Range"); if (!util.isNullOrEmpty(rangeHeader)) { LOG.info("Range header in 308 response is " + rangeHeader); String[] rangeHeaderSplits = rangeHeader.split("-", 2); if (rangeHeaderSplits.length == 2) { previousByte = Long.valueOf(rangeHeaderSplits[1]).longValue() + 1; } } connection = (HttpURLConnection) url.openConnection(); connection.setInstanceFollowRedirects(false); connection.setDoOutput(true); connection.setConnectTimeout(CONNECT_TIMEOUT); connection.setReadTimeout(READ_TIMEOUT); connection.setRequestMethod("PUT"); byte[] bytes; String contentRangeHeader; if (photoEntry.getBlobKey() != null) { long lastByte = previousByte + CHUNK_SIZE; if (lastByte > (photoEntry.getOriginalFileSize() - 1)) { lastByte = photoEntry.getOriginalFileSize() - 1; } contentRangeHeader = String.format("bytes %d-%d/%d", previousByte, lastByte, photoEntry.getOriginalFileSize()); BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); bytes = blobstoreService.fetchData(photoEntry.getBlobKey(), previousByte, lastByte); } else { bytes = dataChunkDao.getBytes(photoEntry.getId(), previousByte); if (bytes == null) { throw new IllegalArgumentException(String.format("PhotoEntry with id '%s' does not " + "have a valid blob key. Additionally, there is no DataChunk entry for the " + "initial byte '%d'.", photoEntry.getId(), previousByte)); } contentRangeHeader = String.format("bytes %d-%d/%d", previousByte, previousByte + bytes.length - 1, photoEntry.getOriginalFileSize()); } connection.setRequestProperty("Content-Length", String.valueOf(bytes.length)); LOG.info("Using the following for Content-Range header: " + contentRangeHeader); connection.setRequestProperty("Content-Range", contentRangeHeader); OutputStream outputStream = connection.getOutputStream(); outputStream.write(bytes); outputStream.close(); if (connection.getResponseCode() == HttpURLConnection.HTTP_CREATED) { LOG.info("Resumable upload is complete and successful."); return (PhotoEntry) ParseUtil.readEntry(new ParseSource(connection.getInputStream())); } } else if (connection.getResponseCode() == HttpURLConnection.HTTP_CREATED) { // It's possible that the Picasa upload associated with the specific resumable upload URL // had previously completed successfully. In that case, the response to the initial */* PUT // will be a 201 Created with the new PhotoEntry. This is probably an edge case. LOG.info("Resumable upload is complete and successful."); return (PhotoEntry) ParseUtil.readEntry(new ParseSource(connection.getInputStream())); } else { // The IllegalArgumentException should be treated by the calling code as // something that is not recoverable, which is to say the resumable upload attempt // should be stopped. throw new IllegalArgumentException(String.format("HTTP POST to %s returned status %d (%s).", url.toString(), connection.getResponseCode(), connection.getResponseMessage())); } } catch (MalformedURLException e) { LOG.log(Level.WARNING, "", e); throw new IllegalArgumentException(e); } catch (IOException e) { LOG.log(Level.WARNING, "", e); } catch (ServiceException e) { LOG.log(Level.WARNING, "", e); } return null; }
From source file:net.sf.eclipsecs.core.config.configtypes.RemoteConfigurationType.java
@Override protected byte[] getBytesFromURLConnection(URLConnection connection) throws IOException { byte[] configurationFileData = null; InputStream in = null;/*from w w w.j a va2s . co m*/ try { // set timeouts - bug 2941010 connection.setConnectTimeout(10000); connection.setReadTimeout(10000); if (connection instanceof HttpURLConnection) { if (!sFailedWith401URLs.contains(connection.getURL().toString())) { HttpURLConnection httpConn = (HttpURLConnection) connection; httpConn.setInstanceFollowRedirects(true); httpConn.connect(); if (httpConn.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) { try { RemoteConfigAuthenticator.removeCachedAuthInfo(connection.getURL()); } catch (CheckstylePluginException e) { CheckstyleLog.log(e); } // add to 401ed URLs sFailedWith401URLs.add(connection.getURL().toString()); throw new IOException(Messages.RemoteConfigurationType_msgUnAuthorized); } } else { // don't retry since we just get another 401 throw new IOException(Messages.RemoteConfigurationType_msgUnAuthorized); } } in = connection.getInputStream(); configurationFileData = IOUtils.toByteArray(in); } finally { IOUtils.closeQuietly(in); } return configurationFileData; }
From source file:com.ontotext.s4.client.HttpClient.java
/** * Handles the sending side of an HTTP request, returning a connection * from which the response (or error) can be read. *//* ww w. j a va 2 s . c o m*/ private HttpURLConnection sendRequest(String target, String method, Object requestBody, String... extraHeaders) throws IOException { URL requestUrl = new URL(baseUrl, target); HttpURLConnection connection = (HttpURLConnection) requestUrl.openConnection(); connection.setRequestMethod(method); connection.setInstanceFollowRedirects(false); connection.setRequestProperty("Authorization", authorizationHeader); boolean sentAccept = false; if (extraHeaders != null) { for (int i = 0; i < extraHeaders.length; i++) { if ("Accept".equals(extraHeaders[i])) sentAccept = true; connection.setRequestProperty(extraHeaders[i], extraHeaders[++i]); } } if (!sentAccept) connection.setRequestProperty("Accept", "application/json"); if (requestBody != null) { connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "application/json"); OutputStream out = connection.getOutputStream(); try { MAPPER.writeValue(out, requestBody); } finally { out.close(); } } return connection; }
From source file:fr.seeks.SuggestionProvider.java
public void setCursorOfQueryThrow(Uri uri, String query, MatrixCursor matrix) throws MalformedURLException, IOException { String url = getUrlFromKeywords(query); Log.v(TAG, "Query:" + url); String json = null;/*from www. jav a2 s .c o m*/ while (json == null) { HttpURLConnection connection = null; connection = (HttpURLConnection) (new URL(url)).openConnection(); try { connection.setDoOutput(true); connection.setChunkedStreamingMode(0); connection.setInstanceFollowRedirects(true); connection.connect(); int response = connection.getResponseCode(); if (response == HttpURLConnection.HTTP_MOVED_PERM || response == HttpURLConnection.HTTP_MOVED_TEMP) { Map<String, List<String>> list = connection.getHeaderFields(); for (Entry<String, List<String>> entry : list.entrySet()) { String value = ""; for (String s : entry.getValue()) { value = value + ";" + s; } Log.v(TAG, entry.getKey() + ":" + value); } // FIXME url = ""; return; } InputStream in = connection.getInputStream(); BufferedReader r = new BufferedReader(new InputStreamReader(in)); StringBuilder builder = new StringBuilder(); String line; while ((line = r.readLine()) != null) { builder.append(line); } json = builder.toString(); /* * Log.v(TAG, "** JSON START **"); Log.v(TAG, json); Log.v(TAG, * "** JSON END **"); */ } catch (IOException e) { e.printStackTrace(); return; } finally { connection.disconnect(); } } JSONArray snippets; JSONObject object; JSONArray suggestions; Boolean show_snippets = mPrefs.getBoolean("show_snippets", false); if (show_snippets) { try { object = (JSONObject) new JSONTokener(json).nextValue(); snippets = object.getJSONArray("snippets"); } catch (JSONException e) { e.printStackTrace(); return; } Log.v(TAG, "Snippets found: " + snippets.length()); for (int i = 0; i < snippets.length(); i++) { JSONObject snip; try { snip = snippets.getJSONObject(i); matrix.newRow().add(i).add(snip.getString("title")).add(snip.getString("summary")) .add(snip.getString("title")).add(Intent.ACTION_SEND).add(snip.getString("url")); } catch (JSONException e) { e.printStackTrace(); continue; } } } else { try { object = (JSONObject) new JSONTokener(json).nextValue(); suggestions = object.getJSONArray("suggestions"); } catch (JSONException e) { e.printStackTrace(); return; } Log.v(TAG, "Suggestions found: " + suggestions.length()); for (int i = 0; i < suggestions.length(); i++) { try { matrix.newRow().add(i).add(suggestions.getString(i)).add("").add(suggestions.getString(i)) .add(Intent.ACTION_SEARCH).add(""); } catch (JSONException e) { e.printStackTrace(); continue; } } } getContext().getContentResolver().notifyChange(uri, null); }
From source file:org.bibsonomy.scraper.url.kde.blackwell.BlackwellSynergyScraper.java
/** FIXME: refactor * Gets the cookie which is needed to extract the content of aip pages. * (changed code from ScrapingContext.getContentAsString) * @param urlConn Connection to api page (from url.openConnection()) * @return The value of the cookie.//from ww w . ja v a2s .co m * @throws IOException */ private String getCookie() throws IOException { HttpURLConnection urlConn = (HttpURLConnection) new URL("http://www.blackwell-synergy.com/help") .openConnection(); String cookie = null; urlConn.setAllowUserInteraction(true); urlConn.setDoInput(true); urlConn.setDoOutput(false); urlConn.setUseCaches(false); urlConn.setFollowRedirects(true); urlConn.setInstanceFollowRedirects(false); urlConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)"); urlConn.connect(); // extract cookie from header Map map = urlConn.getHeaderFields(); cookie = urlConn.getHeaderField("Set-Cookie"); if (cookie != null && cookie.indexOf(";") >= 0) cookie = cookie.substring(0, cookie.indexOf(";")); urlConn.disconnect(); return cookie; }
From source file:com.adaptris.core.http.client.net.StandardHttpProducer.java
private HttpURLConnection configure(HttpURLConnection http, AdaptrisMessage msg) throws Exception { RequestMethod rm = getMethod(msg);//from w ww. ja va 2s. c o m log.trace("HTTP Request Method is : [{}]", rm); http.setRequestMethod(rm.name()); http.setInstanceFollowRedirects(handleRedirection()); http.setDoInput(true); getRequestHeaderProvider().addHeaders(msg, http); String contentType = getContentTypeProvider().getContentType(msg); if (!isEmpty(contentType)) { http.setRequestProperty(CONTENT_TYPE, contentType); } return http; }
From source file:com.gargoylesoftware.htmlunit.UrlFetchWebConnection.java
/** * {@inheritDoc}/*from w w w . j a va2s . c o m*/ */ @Override public WebResponse getResponse(final WebRequest webRequest) throws IOException { final long startTime = System.currentTimeMillis(); final URL url = webRequest.getUrl(); if (LOG.isTraceEnabled()) { LOG.trace("about to fetch URL " + url); } // hack for JS, about, and data URLs. final WebResponse response = produceWebResponseForGAEProcolHack(url); if (response != null) { return response; } // this is a "normal" URL try { final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // connection.setUseCaches(false); connection.setConnectTimeout(webClient_.getOptions().getTimeout()); connection.addRequestProperty("User-Agent", webClient_.getBrowserVersion().getUserAgent()); connection.setInstanceFollowRedirects(false); // copy the headers from WebRequestSettings for (final Entry<String, String> header : webRequest.getAdditionalHeaders().entrySet()) { connection.addRequestProperty(header.getKey(), header.getValue()); } addCookies(connection); final HttpMethod httpMethod = webRequest.getHttpMethod(); connection.setRequestMethod(httpMethod.name()); if (HttpMethod.POST == httpMethod || HttpMethod.PUT == httpMethod || HttpMethod.PATCH == httpMethod) { connection.setDoOutput(true); final String charset = webRequest.getCharset(); connection.addRequestProperty("Content-Type", FormEncodingType.URL_ENCODED.getName()); try (final OutputStream outputStream = connection.getOutputStream()) { final List<NameValuePair> pairs = webRequest.getRequestParameters(); final org.apache.http.NameValuePair[] httpClientPairs = NameValuePair.toHttpClient(pairs); final String query = URLEncodedUtils.format(Arrays.asList(httpClientPairs), charset); outputStream.write(query.getBytes(charset)); if (webRequest.getRequestBody() != null) { IOUtils.write(webRequest.getRequestBody().getBytes(charset), outputStream); } } } final int responseCode = connection.getResponseCode(); if (LOG.isTraceEnabled()) { LOG.trace("fetched URL " + url); } final List<NameValuePair> headers = new ArrayList<>(); for (final Map.Entry<String, List<String>> headerEntry : connection.getHeaderFields().entrySet()) { final String headerKey = headerEntry.getKey(); if (headerKey != null) { // map contains entry like (null: "HTTP/1.1 200 OK") final StringBuilder sb = new StringBuilder(); for (final String headerValue : headerEntry.getValue()) { if (sb.length() != 0) { sb.append(", "); } sb.append(headerValue); } headers.add(new NameValuePair(headerKey, sb.toString())); } } final byte[] byteArray; try (final InputStream is = responseCode < 400 ? connection.getInputStream() : connection.getErrorStream()) { byteArray = IOUtils.toByteArray(is); } final long duration = System.currentTimeMillis() - startTime; final WebResponseData responseData = new WebResponseData(byteArray, responseCode, connection.getResponseMessage(), headers); saveCookies(url.getHost(), headers); return new WebResponse(responseData, webRequest, duration); } catch (final IOException e) { LOG.error("Exception while tyring to fetch " + url, e); throw new RuntimeException(e); } }
From source file:com.gaze.webpaser.StackWidgetService.java
private Bitmap getBitmap(String url) { File f = fileCache.getFile(url); // from SD cache // CHECK : if trying to decode file which not exist in cache return null Bitmap b = decodeFile(f);//from w w w.ja va2 s. c o m if (b != null) return b; // Download image file from web try { Bitmap bitmap = null; URL imageUrl = new URL(url); HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection(); conn.setConnectTimeout(30000); conn.setReadTimeout(30000); conn.setInstanceFollowRedirects(true); InputStream is = conn.getInputStream(); // Constructs a new FileOutputStream that writes to file // if file not exist then it will create file OutputStream os = new FileOutputStream(f); // See Utils class CopyStream method // It will each pixel from input stream and // write pixels to output stream (file) Util.CopyStream(is, os); os.close(); conn.disconnect(); // Now file created and going to resize file with defined height // Decodes image and scales it to reduce memory consumption bitmap = decodeFile(f); return bitmap; } catch (Throwable ex) { ex.printStackTrace(); if (ex instanceof OutOfMemoryError) memoryCache.clear(); return null; } }
From source file:org.jasig.cas.util.SimpleHttpClient.java
@Override public boolean isValidEndPoint(final URL url) { HttpURLConnection connection = null; InputStream is = null;/*from w ww. ja v a 2 s . co m*/ try { connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(this.connectionTimeout); connection.setReadTimeout(this.readTimeout); connection.setInstanceFollowRedirects(this.followRedirects); if (connection instanceof HttpsURLConnection) { final HttpsURLConnection httpsConnection = (HttpsURLConnection) connection; if (this.sslSocketFactory != null) { httpsConnection.setSSLSocketFactory(this.sslSocketFactory); } if (this.hostnameVerifier != null) { httpsConnection.setHostnameVerifier(this.hostnameVerifier); } } connection.connect(); final int responseCode = connection.getResponseCode(); for (final int acceptableCode : this.acceptableCodes) { if (responseCode == acceptableCode) { LOGGER.debug("Response code from server matched {}.", responseCode); return true; } } LOGGER.debug("Response Code did not match any of the acceptable response codes. Code returned was {}", responseCode); // if the response code is an error and we don't find that error acceptable above: if (responseCode == 500) { is = connection.getInputStream(); final String value = IOUtils.toString(is); LOGGER.error("There was an error contacting the endpoint: {}; The error was:\n{}", url.toExternalForm(), value); } } catch (final IOException e) { LOGGER.error(e.getMessage(), e); } finally { IOUtils.closeQuietly(is); if (connection != null) { connection.disconnect(); } } return false; }
From source file:com.amastigote.xdu.query.module.EduSystem.java
@Override public boolean login(String username, String password) throws IOException { preLogin();//ww w. j a v a 2 s. c o m URL url = new URL(LOGIN_HOST + LOGIN_SUFFIX); HttpURLConnection httpURLConnection_a = (HttpURLConnection) url.openConnection(); httpURLConnection_a.setRequestMethod("POST"); httpURLConnection_a.setUseCaches(false); httpURLConnection_a.setInstanceFollowRedirects(false); httpURLConnection_a.setDoOutput(true); String OUTPUT_DATA = "username="; OUTPUT_DATA += username; OUTPUT_DATA += "&password="; OUTPUT_DATA += password; OUTPUT_DATA += "&submit="; OUTPUT_DATA += "<=" + LOGIN_PARAM_lt; OUTPUT_DATA += "&execution=" + LOGIN_PARAM_execution; OUTPUT_DATA += "&_eventId=" + LOGIN_PARAM__eventId; OUTPUT_DATA += "&rmShown=" + LOGIN_PARAM_rmShown; httpURLConnection_a.setRequestProperty("Cookie", "route=" + ROUTE + "; org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE=zh_CN; JSESSIONID=" + LOGIN_JSESSIONID + "; BIGipServeridsnew.xidian.edu.cn=" + BIGIP_SERVER_IDS_NEW + ";"); httpURLConnection_a.connect(); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(httpURLConnection_a.getOutputStream(), "UTF-8"); outputStreamWriter.write(OUTPUT_DATA); outputStreamWriter.flush(); outputStreamWriter.close(); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(httpURLConnection_a.getInputStream())); String html = ""; String temp; while ((temp = bufferedReader.readLine()) != null) { html += temp; } Document document = Jsoup.parse(html); Elements elements = document.select("a"); if (elements.size() == 0) return false; String SYS_LOCATION = elements.get(0).attr("href"); URL sys_location = new URL(SYS_LOCATION); HttpURLConnection httpUrlConnection_b = (HttpURLConnection) sys_location.openConnection(); httpUrlConnection_b.setInstanceFollowRedirects(false); httpUrlConnection_b.connect(); List<String> cookies_to_set = httpUrlConnection_b.getHeaderFields().get("Set-Cookie"); for (String e : cookies_to_set) if (e.contains("JSESSIONID=")) SYS_JSESSIONID = e.substring(11, e.indexOf(";")); httpURLConnection_a.disconnect(); httpUrlConnection_b.disconnect(); return checkIsLogin(username); }