Hi,
I have an urgent ugly problem. I have a php file on a server that reads the name of the files of a directory from the server and POST them to my java application. When I try to read it, i get

Server returned HTTP response code: 400 for URL: 

. This does not happens on files that do not contain white spaces. Only at URLs like: " world.gif" but not at "".
I tried to encode the URL... I could not solve it. Please can you help me? I need it fast...
Thanks!

Dani AI

Generated

Brief summary: the 400 comes from sending an HTTP request line that contains literal spaces. Servers and HTTP clients expect path characters to be percent-encoded (space -> %20); a literal space in the request-line will often be rejected with HTTP 400. saw that only filenames with spaces failed, and tried Java form-encoding which does not solve the path-encoding problem.

Why the common fixes fail

  • java.net.URLEncoder is intended for HTML form encoding (application/x-www-form-urlencoded) and is not the right tool for a URI path. It converts spaces to plus signs and will mangle ":" and "/" if you feed it a whole URL. See the Java docs for details: URLEncoder and the URI rules in RFC 3986.

Reliable Java approach

  • Build a java.net.URI from components so Java will quote illegal characters in the path for you, then convert to a URL/connection. This avoids hand-mangling the whole URL string and handles other reserved characters correctly. Use the multi-argument URI constructor rather than URLEncoder for path segments. See java.net.URI for the constructor behavior.

Quick troubleshooting tips

  • If you only have raw URL strings, parse out scheme/host/path and percent-encode the path only (not the whole URL).
  • Test the final request with curl or a packet capture to see the actual request-line sent to the server.
  • For long-term stability, avoid spaces in filenames on web servers (use hyphens/underscores) and prefer building requests from components rather than editing full URL strings.

Recommended Answers

All 3 Replies

Ah, if only a nickle I had for each urgent problem needing to be solved fast...

/** imports
*/
import java.net.URLDecoder;
import java.net.URLEncoder;

// ...

String addr = " with spaces.gif";
		
String junk = URLEncoder.encode(addr,"UTF-8"); 
System.out.println(junk);
junk = URLDecoder.decode(addr,"UTF-8");
System.out.println(junk);

Your output is:

http%3A%2F%2Fwww.blah.blah%2Fimage+with+spaces.gif
[url] with spaces.gif

Of course, that's quite simple, thanks, but it useless as long as it cannot be parsed and throws malformed URL exception. I had a midi file loaded from URL. I vizited Sun forums and found out some topics and they could not solve this... white spaces in URL are forbidden. The encoding doesn't help as I said and as they posted there, too.
If you encode the URL it becomes unparsable. Sad but true. (same problem on that forum).
However, thanks.

How about trying just replacing the spaces with :

URLString.replaceAll(" ","%20");

I'm personally OK with spaces in filenames, except if they live on a webserver for this very same reason :)

IF you are requesting the filename with a Get or FTP request, then replace the spaces with %20 .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.