public static String extractServerUrl(String urlString){
  try {
    URL url=new URL(urlString);
    String result=url.getProtocol() + "://" + url.getHost();
    if (url.getPort() != -1) {
      result+=":" + url.getPort();
    }
    return result;
  }
 catch (  MalformedURLException e) {
    throw new IllegalArgumentException(e);
  }
}

public static String extractServerUrl ( String urlString ) { try { URL url = new URL ( urlString ) ; String result = url . getProtocol ( ) + "://" + url . getHost ( ) ; if ( url . getPort ( ) != - 1 ) { result += ":" + url . getPort ( ) ; } return result ; } catch ( MalformedURLException e ) { throw new IllegalArgumentException ( e ) ; } }
extract server url from url with path
