To download the file via http (s) I establish a connection like this:

URL connection = new URL(url); HttpURLConnection urlconn; long size; urlconn = (HttpURLConnection) connection.openConnection(); urlconn.setRequestMethod("GET"); size = urlconn.getContentLengthLong(); urlconn.connect(); InputStream in = null; try { in = urlconn.getInputStream(); } catch (IOException e) { e.printStackTrace(); } 

It usually works well, but on some files I get:

java.io.IOException: Server returned HTTP response code: 400 the URL for: http://filecdn.to/fsuaget/Y2VkNTFjOTNkN2UzMDk5NmIyMzljMDBmOGZlZWFmZmJmc3RvfDE2MDQyMDI5MzN8MTAwMDB8MHwwfHw3MDU4MDIzZGEzNDQ4NDI5MTZhN2RiNGI4ZGYxMWNlZHwxfDE0Njg3NTM3NTF8dmlkZW8,/The.Walking.Dead.S06E01.1080p.HDTV.FOX HD.ts

If this URL is inserted into the address bar of the browser (Chrome 51.0.2704.103 m), the file is downloaded normally. As far as I understand, Chrome forms the correct request, I do not. What can be done?

  • If this error is not always, then apparently the case in the server. And the browser simply repeats the request with an error until it is successful. Do the same for you - Yuriy SPb
  • @Yuriy SPb made 10,000 inquiries in a row (~ 10 seconds slapped the server) - the answer is the same - Lurking Elk
  • one
    Try replacing spaces in URLs with %20 - Yuriy SPb
  • @ YuriySPb, exactly) It turned out Chrome does just that - Lurking Elk
  • one
    In such situations, you can use Fiddler to compare requests from the browser and from your application. - Dmitry

2 answers 2

Most likely the fact is that while the browser automatically converts spaces into URLs to %20 , this does not happen to you. Therefore, you need to manually do it - replace all spaces in the URL by %20

  • It turns out the link I get in Exception and the link I entered are different. It seems to be a redirect. It turns out that the URL that the server responds to contains a space, etc. and I can get it only by reading .getMessage() Exception. Is that the way to do? ( - Lurking Elk
  • @DimTeam, try rewriting via OkHttp - there you can set up redirect - maybe this will help. - YurySPb

Symbols other than printable ANSI are written in% NN format, where NN is a hexadecimal character code. For example, the space will be written as % 20 , and the character% as% 25 (see ASCII & ANSI Character Codes ). If Russian Cyrillic characters are not included in the set of printed ANSI characters, they are also replaced in the HTTP headers in the same way.
Use URLEncoder.encode(url,"utf-8") . It converts all non-ANSI characters to% and hexadecimal character codes. This is convenient and you do not need to insert or replace the space with your hands.

  • That's what makes URLEncoder.encode(url,"utf-8") is given in the link above question: http%3A%2F%2Ffilecdn.to%2Ffsuaget%2FY2VkNTFjOTNkN2UzMDk5NmIyMzljMDBmOGZlZWFmZmJmc3RvfDE2MDQyMDI5MzN8MTAwMDB8MHwwfHw3MDU4MDIzZGEzNDQ4NDI5MTZhN2RiNGI4ZGYxMWNlZHwxfDE0Njg3NTM3NTF8dmlkZW8%2C%2FThe.Walking.Dead.S06E01.1080p.HDTV.FOX+HD.ts Not even Chrome understands it - Lurking Elk