URL.openStream fails on redirect to URL with spaces
hi,
silly question probably but i cant find the answer.
a server tries to redirect me to a url with spaces and java doesnt encode the redirected url properly (the second request fails and code 400 is returned)
code like that:
URL url =new URL(urlString);
url.openStream();
on last line IOException is thrown
the communication looks like this
the request is sent from java, the server first replies with HTTP code 302, and returns URL that has empty spaces in the file name part. this is sent as-is back from java in next request and causes server to return HTTP code 400 and java fails with IOException...
as far as i could see other HTTP clients like mozilla or wget encode properly the spaces to %20 in the redirected url (and manage to get the data from redirected url).
what am i doing wrong? please point me to some docs or api pages
thanks & regards
peter
the url, just in case...
http://www.lightclan.nl/index.php?SubID=1079&View=283
[1101 byte] By [
pmscreena] at [2007-11-27 3:54:16]

# 2
well, i pass on to openStream a properly encoded url, then server returns a redirection url with spaces. java code just sends it back as it is, no encoding done. i have no control over what happens between the original request and the redirected one (at least i don't know how to do it).
the http headers look like this
GET /index.php?SubID=1079&View=283 HTTP/1.1
...
HTTP/1.1 302 Found
...
Location: /maps2/Aardwolf Zoological Park.JPG
then the redirected request goes like that:
GET /maps2/Aardwolf Zoological Park.JPG HTTP/1.1
(wrong, this is where i'd expect java libs would substitute the url correctly, no my code executed between the original request and this)
...
HTTP/1.1 400 Bad Request
mozilla or wget encode the second url properly and the url is hit properly then, but not with oepnStream in java
even if it violates some rules of url naming - what should i do to handle this?
thanks for your reply
regards
# 4
rather impossible, sir :) however, as stated before, mozilla, wget, all other clients DO encode it properly with %20 instead of spaces. is there any way to hook in between the first response and the redirected second request and format the redirected url with my own code?thanks
# 7
ok, i found a solution, but would gladly hear some comments on that (correcteness/style etc.)
basically i based this code on JEditorPane.getStream
right now my encodeUrl method just encodes the spaces to %20 with String.replaceAll (it's sufficient for me) but i'd prefer a more 'correct' way. is there any function in java api to encode full url without touching the correct parts (eg. "http://host.org/file name.ext?id=10" => "http://host/file%20name.ext?id=10") ?
thanks and regards
the src code:
public static InputStream openUrlInputStream(URL url) throws IOException {
URLConnection urlConnection = url.openConnection();
if(urlConnection instanceof HttpURLConnection) {
HttpURLConnection httpUrlConnection = (HttpURLConnection)urlConnection;
httpUrlConnection.setInstanceFollowRedirects(false);
int httpResponseCode = httpUrlConnection.getResponseCode();
boolean redirected = ((httpResponseCode >= 300) && (httpResponseCode <=399));
if(redirected) {
String redirectedURLString = urlConnection.getHeaderField("Location");
String encodedUrlString = encodeUrl(redirectedURLString);
URL redirectedUrl;
if(redirectedURLString.startsWith("http")) {
redirectedUrl = new URL(encodedUrlString);
} else {
redirectedUrl = new URL(url, encodedUrlString);
}
return openUrlInputStream(redirectedUrl);
}
}
return urlConnection.getInputStream();
}