URL::createInputStream don't return the correct statusCode


#1

Using URL to send an http request and retrieve the statusCode only (and the response) it fails to get the corret code returning 0 always.

Example:

URL url = getUrl();
bool usePost = isPost(url);
int statusCode = 0;

ScopedPointer<InputStream> is = url.createInputStream (usePost, null, null, String(), 0, null, &statusCode);
String response = is->readEntireStreamAsString();

DBG (String::formatted ("status code: %d, response: %s", statusCode, response));

The ouput of that code is always "status code: 0, response: ..."

I've found, only in the OSX/iOS version to have this problem and it seems to me that the cause is this piece of code (inside WebInputStream constructor in file juce_code/native/juce_mac_Network.mm line 337)

createConnection (progressCallback, progressCallbackContext);

if (responseHeaders != nullptr && connection != nullptr && connection->headers != nil)
{
    statusCode = connection->statusCode;
    ...
}

I change it to this and it seems to work well:

createConnection (progressCallback, progressCallbackContext);

if (connection != nullptr && connection->headers != nil)
{
    statusCode = connection->statusCode;
    
    if (responseHeaders != nullptr) {
        ...
    }
}

Let me know if I'm wrong


#2

Huh? The code already looks like that, and I don't remember changing it recently so it must have been fixed for a while.. You might want to update to the latest version of the library!


#3

Yeah that was one of the fixes I put in my suggested changes for the number-of-redirects stuff - relevant commit is https://github.com/julianstorer/JUCE/commit/268497e8bc2fce53fce43aea720f12c7873d5eb5


#4

I downloaded the last osx package from your website (not from the github repo) and I checked right now, the zip package is outdated. I will now clone the github repo and start using it, as it seems that a lot of bugs were fixed.