Domanda

I have some code that works fine with HTTP, but fails with (null) value returned from the NSJSONObjectserialization-thing on HTTPS.

I tried converting the received data to string, and it comes out to log as valid JSON. I validated it also to be sure. All good. The HTTPS certificate is valid, but I did try implementing the callbacks for invalid certs also, no change.

The EXACT same configuration works perfectly on HTTP, and if I go to the browser and input the same POST (I even tried removing the POST and just have the server return JSON no matter what you input) I get valid JSON also.

I tried converting the received data to an array and grab the first object in the array - didn't work either. I tried changing http headers in different configurations (application/json) etc. No dice.

Error code is Cocoa 3840. This error means, as far as I can tell, invalid JSON. HOW is this possible? It says character 3 is bad?

Any suggestions? I Googled the best I could but all I can find is "https and http should work in the same way" more or less.

Certificate is validated by RapidSSL.

NSDictionary* json = [NSJSONSerialization JSONObjectWithData:_responseDataNews options:kNilOptions error:&error];

_responseDataNews is the object that gets filled by didReceiveData delegate - and this exact config works on HTTP. What the #€%*# am I doing wrong here? It's driving me insane.

Exact error is:

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0xbb2cde0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

If I enable fragment option:

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 3.) UserInfo=0xb939ab0 {NSDebugDescription=Invalid value around character 3.}

JSON looks like: {"r":1,"n":"Text here"} And no, "text" contains no funky characters. This is the actual JSON value I'm testing.

Edit: NSLog of the data object reveals:

efbbbfef bbbfefbb bfefbbbf efbbbf7b 22687322 3a5b7b22 75736572 223a2254
65737465 73656e22 2c227363 6f726522 3a223233 3032227d 5d7d

(with JSON: {"hs":[{"user":"Testesen","score":"2302"}]} )

Edit: ... Okay, as it turns out, now it doesn't work on HTTP either, so it must be after changing the SSL setup or something on the server that it broke!? HTTP returns the exact same (invalid) data object :(

È stato utile?

Soluzione

You report that the NSData for your string is:

<efbbbfef bbbfefbb bfefbbbf efbbbf7b 22687322 3a5b7b22 75736572 223a2254
 65737465 73656e22 2c227363 6f726522 3a223233 3032227d 5d7d>

The ef bb bf is a byte order mark for a UTF-8 string. But if you have a BOM, you should have only one, and you have five of them.

If you're creating this response programmatically, you may have some server function that is called multiple times that is adding this BOM repeatedly. The BOM should appear only once (if at all), and the fact that it's appearing multiple times indicates a problem in your server code.

If you created this file manually, you might want to edit the hex data. Use whatever hex editor you want to fix this. For example, if you want to edit this file with Xcode, you can add it to your Xcode project and then right click on the file and choose "Open as..." - "Hex".

Altri suggerimenti

Okay - it turns out this had nothing to do with HTTPS anyway.

For some reason I cannot really understand, my php script was prefixing invalid characters after I restructured it to use only 1 .php file that responds to POST, includes all other .php files and returns the appropriate function depending on what you POST - of course I tried to just make it call one of the functions without POSTing anything, so that I could exclude POST errors from troubleshooting.

After I changed it so I request the specific file depending on what I want to do on the server - get news, get users, get score, whatever, like server.com/getscore.php instead of just server.com + POST data to get score, it works without issue and the JSON response is valid.

I don't really know how .php messes this up, but SOMETHING goes wrong. I tried enabling "Show all characters" in Notepad++, but I don't see anything funky anywhere. I made sure all .php files are encoded in UTF-8 also.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top