1

I'm getting "Server returned HTTP response code: 505 for URL" for the below code:

                URL url = new URL(fileLocations.get(i));
                URLConnection conn = url.openConnection();
                InputStream in = conn.getInputStream();

While the URL works fine when opened in browser. Also tried encoding the url, that too didn't work.

URL is : http://52.66.123.140:8080/TATADHPFILES/1239/TDH Items/149387773752120170504_113201.jpg

What may be the cause?

Divya Nagrath
  • 139
  • 1
  • 2
  • 9

3 Answers3

1

The 505 error is "HTTP Error 505 HTTP version not supported" (which may be related to "java.net. URISyntaxException : Malformed IPv6 address").

I resolved your issue by encoding (the URL), and wrapping in a URI:

public static void main(String args[]) throws IOException, URISyntaxException {
    URI uri = new URI(
            "http",
            "52.66.123.140:8080",
            "/TATADHPFILES/1239/TDH Items/149387773752120170504_113201.jpg",
            "Implementation", "Java");
    URL url = uri.toURL();

    try {
        BufferedImage img = ImageIO.read(url);

        // --- your original code will also now work ---
        URLConnection conn = url.openConnection();
        InputStream in = conn.getInputStream();
        // ---------------------------------------

        System.out.println("tester");
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}

I was able to set a breakpoint (using Intellij) on System.out.println("tester"); - and was able to view the img variable (displaying the "correct" image).

Your original code will also work.

chocksaway
  • 870
  • 1
  • 10
  • 21
0

I think the root cause of the issue is that you encodes whole string instead of encoding only params :

 String urlString = "someurl?param1=" + URLEncoder.encode(first, "UTF-8") + "&param2=" + URLEncoder.encode(second, "UTF-8") ;
ema
  • 891
  • 11
  • 21
0

Encode your url with URLEncoder

URL url = new URL(fileLocations.get(i));
String encodedURL=java.net.URLEncoder.encode(url,"UTF-8");
System.out.println(encodedURL); 
Jens
  • 67,715
  • 15
  • 98
  • 113
syam
  • 799
  • 1
  • 12
  • 30