3

I'm trying to access and download an image hosted on Google Cloud Storage with a signed URL, but it's throwing a MalformedURLException if I pass the signed URL string along as a parameter. I know it's a valid URL, as pasting it into my browser works.

This doesn't work:

URL url = new URL(signedUrl);

However, pasting the output of System.out.println(signedUrl) into the URL object does work.

System.out.println(signedUrl);
URL url = new URL("https://storage.googleapis.com/rydr/insurance_documents/test123.png?X...");

The exception:

java.lang.RuntimeException: java.net.MalformedURLException: no protocol: "https://storage.googleapis.com/rydr/insurance_documents/test123.png?X..."

Several years ago, a StackOverflow user had a similar problem here, but the suggestions of using .trim() failed to resolve the issue for me.

I also tried using DownloadManager, but it throws a similar exception:

java.lang.IllegalArgumentException: Can only download HTTP/HTTPS URIs: "https://storage.googleapis.com/rydr/insurance_documents/test123.png?X..."
2
  • Clearly there is an invisible character in https:. Check.
    – user207421
    Commented Mar 20 at 7:21
  • 1
    "I know it's a valid URL, as pasting it into my browser works." - That's not a definitive test. A typical browser's URL bar will silently "fix" various problems in the URL to make it valid. The URL class is not as forgiving. If URL says it is invalid, then it >is< invalid.
    – Stephen C
    Commented Mar 20 at 7:39

1 Answer 1

2

After viewing the PHP script which generates the signed URL in the browser, I found that it was outputting the URL inside double quotes.

Manually removing the double quotes in Java fixed the problem:

URL url = new URL(signedUrl.replace("\"", ""));
1
  • 2
    Better still fix the PHP, the source of the problem. Otherwise you are just writing software with a thousand crutches.
    – user207421
    Commented Mar 20 at 8:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.