Here you can find the source of getBaseURL(URLConnection conn)
static public String getBaseURL(URLConnection conn)
//package com.java2s; /******************************************************************************* * Copyright (c) 2005-2011 eBay Inc.//w w w. j ava2s . c o m * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * *******************************************************************************/ import java.net.URLConnection; public class Main { /** * get the base URL from a connection */ static public String getBaseURL(URLConnection conn) { String url = conn.getURL().toExternalForm(); return getBaseURL(url); } /** * Returns the base URL with ending slash */ static public String getBaseURL(String url_str) { if (url_str == null) return null; String s = url_str; int i = s.lastIndexOf(';'); if (i > -1) s = s.substring(0, i); i = s.lastIndexOf('#'); if (i > -1) s = s.substring(0, i); i = s.lastIndexOf('?'); if (i > -1) s = s.substring(0, i); i = s.indexOf("://"); if (i > 0) i += 4; int j = s.lastIndexOf('/'); if (j > i) { s = s.substring(0, j); } if (!s.endsWith("/")) s = s + "/"; return s; } }