List of usage examples for android.net WebAddress WebAddress
public WebAddress(String address) throws ParseException
From source file:android.net.http.RequestHandle.java
/** * Create and queue a redirect request./*w w w .j a v a 2 s . c om*/ * * @param redirectTo URL to redirect to * @param statusCode HTTP status code returned from original request * @param cacheHeaders Cache header for redirect URL * @return true if setup succeeds, false otherwise (redirect loop * count exceeded, body provider unable to rewind on 307 redirect) */ public boolean setupRedirect(String redirectTo, int statusCode, Map<String, String> cacheHeaders) { if (HttpLog.LOGV) { HttpLog.v("RequestHandle.setupRedirect(): redirectCount " + mRedirectCount); } // be careful and remove authentication headers, if any mHeaders.remove(AUTHORIZATION_HEADER); mHeaders.remove(PROXY_AUTHORIZATION_HEADER); if (++mRedirectCount == MAX_REDIRECT_COUNT) { // Way too many redirects -- fail out if (HttpLog.LOGV) HttpLog.v("RequestHandle.setupRedirect(): too many redirects " + mRequest); mRequest.error(EventHandler.ERROR_REDIRECT_LOOP, com.android.internal.R.string.httpErrorRedirectLoop); return false; } if (mUrl.startsWith("https:") && redirectTo.startsWith("http:")) { // implement http://www.w3.org/Protocols/rfc2616/rfc2616-sec15.html#sec15.1.3 if (HttpLog.LOGV) { HttpLog.v("blowing away the referer on an https -> http redirect"); } mHeaders.remove("Referer"); } mUrl = redirectTo; try { mUri = new WebAddress(mUrl); } catch (ParseException e) { e.printStackTrace(); } // update the "Cookie" header based on the redirected url mHeaders.remove("Cookie"); String cookie = CookieManager.getInstance().getCookie(mUri); if (cookie != null && cookie.length() > 0) { mHeaders.put("Cookie", cookie); } if ((statusCode == 302 || statusCode == 303) && mMethod.equals("POST")) { if (HttpLog.LOGV) { HttpLog.v("replacing POST with GET on redirect to " + redirectTo); } mMethod = "GET"; } /* Only repost content on a 307. If 307, reset the body provider so we can replay the body */ if (statusCode == 307) { try { if (mBodyProvider != null) mBodyProvider.reset(); } catch (java.io.IOException ex) { if (HttpLog.LOGV) { HttpLog.v("setupRedirect() failed to reset body provider"); } return false; } } else { mHeaders.remove("Content-Type"); mBodyProvider = null; } // Update the cache headers for this URL mHeaders.putAll(cacheHeaders); createAndQueueNewRequest(); return true; }
From source file:com.android.browser.DownloadHandler.java
private static void onDownloadNoStreamImpl(Activity activity, String url, String userAgent, String contentDisposition, String mimetype, String referer, boolean privateBrowsing) { String filename = URLUtil.guessFileName(url, contentDisposition, mimetype); // Check to see if we have an SDCard String status = Environment.getExternalStorageState(); if (!status.equals(Environment.MEDIA_MOUNTED)) { int title; String msg;/*w w w . j av a 2s. co m*/ // Check to see if the SDCard is busy, same as the music app if (status.equals(Environment.MEDIA_SHARED)) { msg = activity.getString(R.string.download_sdcard_busy_dlg_msg); title = R.string.download_sdcard_busy_dlg_title; } else { msg = activity.getString(R.string.download_no_sdcard_dlg_msg, filename); title = R.string.download_no_sdcard_dlg_title; } new AlertDialog.Builder(activity).setTitle(title).setIconAttribute(android.R.attr.alertDialogIcon) .setMessage(msg).setPositiveButton(R.string.ok, null).show(); return; } // java.net.URI is a lot stricter than KURL so we have to encode some // extra characters. Fix for b 2538060 and b 1634719 WebAddress webAddress; try { webAddress = new WebAddress(url); webAddress.setPath(encodePath(webAddress.getPath())); } catch (Exception e) { // This only happens for very bad urls, we want to chatch the // exception here Log.e(LOGTAG, "Exception trying to parse url:" + url); return; } String addressString = webAddress.toString(); Uri uri = Uri.parse(addressString); final DownloadManager.Request request; try { request = new DownloadManager.Request(uri); } catch (IllegalArgumentException e) { Toast.makeText(activity, R.string.cannot_download, Toast.LENGTH_SHORT).show(); return; } request.setMimeType(mimetype); // set downloaded file destination to /sdcard/Download. // or, should it be set to one of several Environment.DIRECTORY* dirs depending on mimetype? try { request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename); } catch (IllegalStateException ex) { // This only happens when directory Downloads can't be created or it isn't a directory // this is most commonly due to temporary problems with sdcard so show appropriate string Log.w(LOGTAG, "Exception trying to create Download dir:", ex); Toast.makeText(activity, R.string.download_sdcard_busy_dlg_title, Toast.LENGTH_SHORT).show(); return; } // let this downloaded file be scanned by MediaScanner - so that it can // show up in Gallery app, for example. request.allowScanningByMediaScanner(); request.setDescription(webAddress.getHost()); // XXX: Have to use the old url since the cookies were stored using the // old percent-encoded url. String cookies = CookieManager.getInstance().getCookie(url, privateBrowsing); request.addRequestHeader("cookie", cookies); request.addRequestHeader("User-Agent", userAgent); if (!TextUtils.isEmpty(referer)) { request.addRequestHeader("Referer", referer); } request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); if (mimetype == null) { if (TextUtils.isEmpty(addressString)) { return; } // We must have long pressed on a link or image to download it. We // are not sure of the mimetype in this case, so do a head request new FetchUrlMimeType(activity, request, addressString, cookies, userAgent).start(); } else { final DownloadManager manager = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE); new Thread("Browser download") { public void run() { manager.enqueue(request); } }.start(); } Toast.makeText(activity, R.string.download_pending, Toast.LENGTH_SHORT).show(); }
From source file:android.webkit.LoadListener.java
/** * Sets the current URL associated with this load. *///ww w . j a va2 s .c om void setUrl(String url) { if (url != null) { if (URLUtil.isDataUrl(url)) { // Don't strip anchor as that is a valid part of the URL mUrl = url; } else { mUrl = URLUtil.stripAnchor(url); } mUri = null; if (URLUtil.isNetworkUrl(mUrl)) { try { mUri = new WebAddress(mUrl); } catch (ParseException e) { e.printStackTrace(); } } } }