954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Server returned HTTP response code: 400 for URL. CANNOT WHITE SPACE URL

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: http://www.xxxxxxxxxxxxx


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

Clawsy
Posting Whiz in Training
225 posts since Feb 2008
Reputation Points: 11
Solved Threads: 7
 

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 = "http://www.blah.blah/image 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
<a href="http://www.blah.blah/image">http://www.blah.blah/image</a> with spaces.gif

PatrickSharp
Newbie Poster
23 posts since Feb 2010
Reputation Points: 13
Solved Threads: 6
 

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.

Clawsy
Posting Whiz in Training
225 posts since Feb 2008
Reputation Points: 11
Solved Threads: 7
 

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 .

PatrickSharp
Newbie Poster
23 posts since Feb 2010
Reputation Points: 13
Solved Threads: 6
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: