I'm having a really bizarre problem with URLConnection.getInputStream() when I have a space (' ') in the query string portion of a URI. Specifically, I have one URL that works and another that does not, when I think they should both fail or both succeed, additionally, its every time.
Working URL: http://minneapolis.craigslist.ca/search/sss?catAbb=sss&query=iPhone+sprint&sort=date&srchType=A&format=rss Failed URL (exception below) : http://winnipeg.craigslist.ca/search/sss?catAbb=sss&query=iPhone+sprint&sort=date&srchType=A&format=rss
conn.getInputStream() throws the IO exception: "Illegal character in query at index 67: http://winnipeg.en.craigslist.ca/search/sss?catAbb=sss&query=iPhone sprint two&sort=date&srchType=A&format=rss"
It appears openConnection can't get the space (which I've already replaced with a '+' as I'd expect to have to with a 'URL', I've also tried '%20' with the same results.
Additionally, URL.toString() reports the URLS as I printed above, with the '+' not the space.
Code is as follows, searchUrl is a 'URL' instance.
URLConnection conn = null;
conn = searchUrl.openConnection();
conn.setConnectTimeout(CONNECT_TIMEOUT);
conn.setUseCaches(true);
conn.setAllowUserInteraction(false);
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
InputStream istream = conn.getInputStream();
int numBytesRead, numBytesWritten = 0;
byte[] buffer = new byte[8 * 1024];
while ((numBytesRead = istream.read(buffer, 0, 8 * 1024)) > 0) {
oStream.write(buffer, numBytesWritten, numBytesRead);
numBytesWritten += numBytesRead;
}
Any ideas on where to deal with this? I'm about to pitch URLConnection and go another route...
Thanks Kenny.