Have a look at http://docs.oracle.com/javase/1.5.0/docs/api/java/net/URLEncoder.html
It is not only the blanks that need to be replaced. URLs consist of special caracters for special meanings (e.g. the question mark to start the query string)
String flag1 = URLEncoder.encode("This string has spaces", "UTF-8")